我有一个 MKMapView,每次加载 View 时或调用 showLocation
自定义类方法时,我都会删除一个注释。
我需要最好的准确性
-(void)viewDidLoad {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
[locationManager startUpdatingLocation];
}
-(IBAction) showLocation:(id) sender{
[locationManager startUpdatingLocation];
}
- (void) locationManager:(CLLocationManager *) manager
didUpdateToLocation:(CLLocation *) newLocation
fromLocation:(CLLocation *) oldLocation {
// start geocoding with newLocation coordinate which will automatically set annotation.
SVGeocoder *geocodeRequest = [[SVGeocoder alloc]
initWithCoordinate:CLLocationCoordinate2DMake(newLocation.coordinate.latitude, newLocation.coordinate.longitude)];
[geocodeRequest setDelegate:self];
[geocodeRequest startAsynchronous];
[geocodeRequest release];
[locationManager stopUpdatingLocation];
}
我的问题是何时调用didUpdateToLocation
方法?仅当我执行 [locationManager startUpdatingLocation]
时找到新位置后?
当用户旅行和静止时,我遇到一些奇怪的问题。 假设用户从 A->B->C->D 点出发,各点之间的间隔为 1 分钟。当我在 C 点调用我的方法时,有时它返回 A 点的坐标,有时返回 B 点,有时返回 C 点的坐标。这只是随机的。
当我静止不动时,情况就更奇怪了。当我调用 showLocation
方法时,即使我连接了家里的 WiFi,我也会得到不同的坐标。
我正在考虑实现 didUpdateToLocation 以在 5 秒内获得最佳结果。如果在 5 秒内,它找到了我定义的精度的特定位置,则使用该坐标。如果没有,请使用 5 秒时间范围内找到的最佳值。但由于我是新手,我不确定如何编写类似的代码。我阅读了 NSTimer,看起来它可能有效。
大家有什么建议吗?
提前致谢!
最佳答案
您从 A 点接收位置的原因之一是 CoreLocation 首先返回它所拥有的最后一个有效位置,直到它可以获得更准确的位置。当您调用[locationManager startUpdatingLocation];
时,它会一遍又一遍地返回-didUpdateToLocation
,直到您满意并最终调用-stopUpdatingLocation
。
我认为您只需要在停止更新位置之前留出一些时间来获得更好的位置修复。我会考虑将停止更新位置从 -didUpdateToLocation
移至其他方法。
关于iphone - CLLocationManager 奇怪的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7307494/