首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CLLocationDistance:无法让"getDistanceFrom“正常工作

CLLocationDistance:无法让"getDistanceFrom“正常工作
EN

Stack Overflow用户
提问于 2010-02-10 02:48:07
回答 2查看 3.6K关注 0票数 0

基于视图的应用程序,

代码语言:javascript
复制
.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]);
}

怎么啦?

调试器说:

代码语言:javascript
复制
obj_msgSend
0x912e8688  <+0024>  mov    0x20(%edx),%edi

来自数据库的对象:长整型: 59.291114长整型: 17.816191

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-02-10 03:40:46

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

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

还有建筑..。

代码语言:javascript
复制
NSLog([NSString stringWithFormat:@"%.0f", distance]);

..。是危险和多余的。使用...

代码语言:javascript
复制
NSLog(@"%.0f", distance);

..。而不是。

票数 2
EN

Stack Overflow用户

发布于 2010-05-30 04:59:47

请改用distanceFromLocation:

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2231584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档