最近遇到一个不可思议的问题,用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秒以上的就被列为旧值。