我试图在应用程序的一开始就使用 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/