对于安卓手机,无论您当前的定位服务是否开启,您都可以检索到最后可用的位置点,这非常有用,因为您可以在最新位置之前根据它进行猜测可用。
我如何为 iPhone 做到这一点?假设当我尝试访问 iOS 位置服务时,用户已经在室内行走。因此,最新的位置不可用。我是否可以检索用户上次暴露在天空下的位置?
谢谢!
最佳答案
不知道有没有开箱即用的方法可以直接使用。但在我的应用程序中,我使用了变通方法。我只是像这样将最近的用户位置保存到 NSUserDefault 中
- (void)setLastSavedLocation:(CLLocation *)location
{
[[NSUserDefaults standardUserDefaults] setDouble:location.coordinate.latitude forKey:@"kLocationManagerLatKey"];
[[NSUserDefaults standardUserDefaults] setDouble:location.coordinate.longitude forKey:@"kLocationManagerLngKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
我会调用它来获取数据:
- (CLLocation *)lastSavedLocation
{
if (![[NSUserDefaults standardUserDefaults] objectForKey:@"kLocationManagerLatKey"]) return nil;
return [[CLLocation alloc] initWithLatitude:[[NSUserDefaults standardUserDefaults] doubleForKey:@"kLocationManagerLatKey"]
longitude:[[NSUserDefaults standardUserDefaults] doubleForKey:@"kLocationManagerLngKey"]];
}
希望这能给你一些解决这个问题的想法。
关于ios - iphone - 位置服务,最后可用的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21809998/