【问题标题】:CLLocationDistance: Cant get "getDistanceFrom" to workCLLocationDistance:无法让“getDistanceFrom”工作
【发布时间】:2010-02-09 18:48:06
【问题描述】:

基于视图的应用,

.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

【问题讨论】:

    标签: iphone objective-c cllocationmanager cllocation


    【解决方案1】:

    改用 distanceFromLocation:

    【讨论】:

      【解决方案2】:

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

      如果它是一个属性,请使用self.CurrentLocation 来确保它被保留。

      还有,建筑...

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

      ... 是有风险和多余的。使用...

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

      ... 相反。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-23
        • 2014-12-26
        • 2017-12-22
        • 2012-02-28
        • 2015-12-01
        • 2012-01-29
        • 2014-12-16
        • 2013-04-19
        相关资源
        最近更新 更多