CLLocationManager距离位置:返回-1

发布于 2024-10-21 18:14:47 字数 1871 浏览 3 评论 0原文

我试图在应用程序的一开始就使用 corelocation 来获取初始位置。

我有委托方法:

-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    NSDate* eventDate = newLocation.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];

    DLog(@"received location data from %f ago, lat/long - %+.6f, %+.6f\nold lat/long %+.6f, %+.6f\nhorizontal accuracy is %f", 
     howRecent, newLocation.coordinate.latitude, newLocation.coordinate.longitude, 
     oldLocation.coordinate.latitude, oldLocation.coordinate.longitude, 
     newLocation.horizontalAccuracy);

    // check if new update is significantly different from old one.  If it is, then reverse-geocode.
    if ([newLocation respondsToSelector:@selector(distanceFromLocation:)]) {
        DLog(@"distance from old to new location - %f", [newLocation distanceFromLocation:oldLocation]);
        if ([newLocation distanceFromLocation:oldLocation] < 1000) {
            // reverse geocode please
            [self stopLocationManager];
        }
    }

    else if ([newLocation respondsToSelector:@selector(getDistanceFrom:)]){
        DLog(@"distance from old to new location - %f", [newLocation getDistanceFrom:oldLocation]);
        if ([newLocation getDistanceFrom:oldLocation] < 1000) {
            // reverse geocode please
            [self stopLocationManager];
        }
    }
}

这是日志输出:

... -[AppHelper locationManager:didUpdateToLocation:fromLocation:] received location data from -13.261873 ago, lat/long - +37.705413, -121.866296
old lat/long +0.000000, +0.000000
horizontal accuracy is 1982.000000
... -[AppHelper locationManager:didUpdateToLocation:fromLocation:] distance from old to new location - -1.000000

知道为什么从 (0,0) 的新位置到旧位置的初始距离计算会产生 -1 结果吗?

I am trying to use corelocation at the very beginning of my app, to get initial location.

I have delegate method:

-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    NSDate* eventDate = newLocation.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];

    DLog(@"received location data from %f ago, lat/long - %+.6f, %+.6f\nold lat/long %+.6f, %+.6f\nhorizontal accuracy is %f", 
     howRecent, newLocation.coordinate.latitude, newLocation.coordinate.longitude, 
     oldLocation.coordinate.latitude, oldLocation.coordinate.longitude, 
     newLocation.horizontalAccuracy);

    // check if new update is significantly different from old one.  If it is, then reverse-geocode.
    if ([newLocation respondsToSelector:@selector(distanceFromLocation:)]) {
        DLog(@"distance from old to new location - %f", [newLocation distanceFromLocation:oldLocation]);
        if ([newLocation distanceFromLocation:oldLocation] < 1000) {
            // reverse geocode please
            [self stopLocationManager];
        }
    }

    else if ([newLocation respondsToSelector:@selector(getDistanceFrom:)]){
        DLog(@"distance from old to new location - %f", [newLocation getDistanceFrom:oldLocation]);
        if ([newLocation getDistanceFrom:oldLocation] < 1000) {
            // reverse geocode please
            [self stopLocationManager];
        }
    }
}

Here's the log output:

... -[AppHelper locationManager:didUpdateToLocation:fromLocation:] received location data from -13.261873 ago, lat/long - +37.705413, -121.866296
old lat/long +0.000000, +0.000000
horizontal accuracy is 1982.000000
... -[AppHelper locationManager:didUpdateToLocation:fromLocation:] distance from old to new location - -1.000000

Any idea why initial distance calculation from newlocation to oldlocation of (0,0) is yielding -1 result?

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

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

发布评论

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

评论(2

睡美人的小仙女 2024-10-28 18:14:47

看起来距离是-1,因为旧位置(0,0) 无效。我会忽略第一个距离(-1)并等待下一个距离。

对我来说,第一个位置不能有旧位置是完全有道理的。

It seems like the distance is -1, because the old location (0,0) is invalid. I would ignore the first distance (-1) and wait for the next one.

To me it makes perfect sense that the first location can't have an old location.

那小子欠揍 2024-10-28 18:14:47

天哪,我第二次回答我自己的问题..抱歉,我猜我调试得不够!

所以这就是发生的事情 - 当第一次调用 delegate 时,看起来 oldLocation 可以用 nil 调用。

因此,distanceFromLocation: 是用 nil 参数调用的,并返回 -1 作为某种错误代码。

不管怎样,我现在只是添加一个检查来查看 oldlocation 是否为零,并忽略第二个更新。

如果 distanceFromLocation: 在使用 nil 参数调用时实际上触发某种断言,而不是神秘的 -1 错误代码,那就太好了! :D

Gosh, second time I'm answering my own question.. sorry I didn't debug enough i guess!

So here's what's happening - when delegate is called first time, it looks like oldLocation can be called with nil.

Thus, distanceFromLocation: is called with nil argument, and returning -1 as some sort of error code.

Anyway, I am now just adding a check to see if oldlocation is nil, and ignore that update for second one.

Would have been nice if distanceFromLocation: actually fires an assertion of some sort when called with nil argument, instead of cryptic -1 error code! :D

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