iphone - CLLocationManager距离位置: returns -1

标签 iphone distance cllocationmanager

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

我有委托(delegate)方法:

-(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 结果吗?

最佳答案

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

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

关于iphone - CLLocationManager距离位置: returns -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5282506/

相关文章:

ios - xcode 6.1 iphone/ipad Storyboard

iphone - 如果该应用程序是通用的,为什么我不能在App Store中看到我的iPad版本的应用程序?

iphone - 在 iOS 7 中获取 NSString 高度

python - 如何在python中找到两个numpy数组的最近点

iOS CLLocationManager - 确定位置后如何调用方法

ios - 如何在iPhone的背景模式下获取当前位置?

iphone - 应用程序是否应该开始位置跟踪以便从 CLLocationManager 获取任何最后已知的位置?

iphone - 如何将 UIPickerView 重置为索引 :0, iPhone

ios - 如何根据单元格 subview (自定义添加的标签)的值对 UITableView 中的行进行正确排序?

python - 在 Python 中给定经纬度数据计算距离矩阵的有效方法