CLLocationDistance:无法使“getDistanceFrom”起作用

时间:2010-02-09 18:48:07

标签: iphone objective-c cllocationmanager cllocation

基于视图的应用

.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

2 个答案:

答案 0 :(得分:4)

使用 distanceFromLocation:代替

答案 1 :(得分:2)

CurrentLocation是实例变量/属性吗?您是在没有访问者的情况下直接设置的,并且您没有保留它。如果在getDistance:中使用它之前将其释放,则会导致崩溃。

如果是属性,请使用self.CurrentLocation以确保其被保留。

另外,建筑......

NSLog([NSString stringWithFormat:@"%.0f", distance]);

......风险多余。使用...

NSLog(@"%.0f", distance);

......相反。