{
...
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
NSLog(@"myLocation1: %@",[locations lastObject]);
myLocation = [locations lastObject];
NSLog(@"myLocation2: %@",[locations lastObject]);
[manager stopUpdatingLocation];
[self doSomethingWithLocation];
}
目前我在位置 40.000,40.000。 我要关闭我的应用程序并将位置更改为 10.000,10.000
再次进入应用程序并运行 [locationManager startUpdatingLocation];
我的日志将显示:
myLocation1: <+40.00000000,+40.00000000>
myLocation2: <+40.00000000,+40.00000000>
如果我再次触发 [locationManager startUpdatingLocation];
我的日志将显示:
myLocation1: <+10.00000000,+10.00000000>
myLocation2: <+10.00000000,+10.00000000>
如何调用一次 didUpdateLocations
并仍然获取当前位置?
我应该使用另一个委托(delegate)吗?
我想我可以将 stopUpdatingLocation
放在 doSomethingWithLocation
中并在某种延迟后运行 doSomethingWithLocation
以便更新正确的位置但是我敢肯定这不是它的本意。
谢谢
最佳答案
让位置管理器运行一段时间(例如 30 秒),设置一个计时器告诉它停止。位置管理器更新就像薄煎饼,您得到的第一个并不总是最好的。
关于ios - 仅在第二次调用 startUpdatingLocation 后获取当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18857510/