CLLocationDistance: Cant get “getDistanceFrom” para trabajar
-
19-09-2019 - |
Pregunta
aplicación basada vista,
.m
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
[[self locationManager] startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
CurrentLocation = newLocation;
[locationManager stopUpdatingLocation];
myActivity.hidden=YES;
[self reStart];
}
-(void)reStart{
[self checkAndCreateDatabase]; //Sucess
[self readFromDatabase]; //Success if I comment the get distance part
[self sort]; //Success
}
-(void) readFromDatabase {
...
CLLocationCoordinate2D cord;
cord.latitude = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)] doubleValue];
cord.longitude =[[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)] doubleValue];
NSDate *today = [NSDate date];
CLLocation *objectLocation = [[CLLocation alloc] initWithCoordinate:cord altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:today];
CLLocationDistance distance = [objectLocation getDistanceFrom:CurrentLocation]/1000;
NSLog([NSString stringWithFormat:@"%.0f", distance]);
}
¿Qué está mal?
depurador dice:
obj_msgSend
0x912e8688 <+0024> mov 0x20(%edx),%edi
objeto a partir de la base de datos: de largo: 59.291114 lat: 17.816191
Solución
Es CurrentLocation
una instancia variable / propiedad? Está tratando de ajustar directamente sin un descriptor de acceso y no se está reteniendo. Si se libera antes de usarla en getDistance:
va a causar un accidente.
Si se trata de una propiedad de utilizar el self.CurrentLocation
para asegurar que se mantiene.
Además, la construcción ...
NSLog([NSString stringWithFormat:@"%.0f", distance]);
... es arriesgado y redundante. Uso ...
NSLog(@"%.0f", distance);
... en su lugar.
Otros consejos
Uso distanceFromLocation: en lugar
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow