CLLocationDistance:不能让“getDistanceFrom”工作
-
19-09-2019 - |
题
查看基于应用程序,
.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]);
}
什么是错的?
调试器表示:
obj_msgSend
0x912e8688 <+0024> mov 0x20(%edx),%edi
从数据库对象: 长:59.291114 纬度:17.816191
解决方案
时CurrentLocation
一个实例变量/属性?您的情况下直接访问器设置它,你不保留它。如果您在getDistance:
使用它之前,它被释放,将导致崩溃。
如果它是一个属性使用self.CurrentLocation
以确保它被保留。
另外,施工...
NSLog([NSString stringWithFormat:@"%.0f", distance]);
...是有风险的和冗余的。使用...
NSLog(@"%.0f", distance);
...来代替。
其他提示
使用的 distanceFromLocation:强>而不是
不隶属于 StackOverflow