CLLocationDistance:无法获取“getDistanceFrom”去工作

发布于 2024-08-21 05:48:48 字数 1515 浏览 9 评论 0原文

基于视图的应用程序,

.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

view based app,

.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]);
}

What is wrong?

debugger says:

obj_msgSend
0x912e8688  <+0024>  mov    0x20(%edx),%edi

object from database:
long: 59.291114
lat: 17.816191

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

同展鸳鸯锦 2024-08-28 05:48:48

使用 distanceFromLocation: 代替

Use distanceFromLocation: instead

时常饿 2024-08-28 05:48:48

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

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

而且,建设……

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

是有风险和多余的。用……

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

代替。

Is CurrentLocation an instance-variable/property? You are setting it directly without an accessor and you are not retaining it. If it is released before you use it in getDistance: it will cause a crash.

If it is a property use the self.CurrentLocation to ensure it is retained.

Also, the construction...

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

... is risky and redundant. Use...

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

... instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文