最近遇到一个不可思议的问题,用CLLocationManager更新位置的时候会返回旧的位置信息,这是为什么呢,经过google才知道原来是它有一个cache的功能,所以会返回cache值。那返回给我们旧的值有何用,反而是个麻烦事,会让annotationview在地图上跳来跳去。
如何解决这个问题,其实官方也说明了这种情况,也给出了解决方案, 下面就是官方的解决方案:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSDate* newLocDate = newLocation.timestamp;
NSTimeInterval interval = [newLocDate timeIntervalSinceNow];
if( abs(interval) < 10 ) {
self.location = newLocation;
[locationManager stopUpdatingLocation];
locationManager.delegate = nil;
[ ... ];
} else {
//skip the cache data
}
}
这个解决方案就是利用CLLocation里的timestamp属性,如果时间离当前时间有10秒以上的就被列为旧值。
本文探讨了使用CLLocationManager更新位置时遇到位置信息滞后的问题,解释了其背后的原因在于缓存机制,并提供了官方建议的解决方案。通过检查位置更新的时间戳与当前时间的间隔,可以有效过滤掉过时的数据,确保地图上的标注及时准确。
6806

被折叠的 条评论
为什么被折叠?



