【问题标题】:iphone - location service, last available location?iphone - 定位服务,最后可用的位置?
【发布时间】:2014-02-16 10:33:06
【问题描述】:

对于 android 手机,无论您当前的位置服务是否开启,您都可以检索最后一个可用的位置点,这非常有帮助,因为您可以在最新位置出现之前根据它进行猜测可用。

我怎样才能为 iPhone 做到这一点?假设当我尝试访问 iOS 定位服务时,用户已经走进了室内。因此,最新的位置不可用。我可以检索用户最后一次暴露在天空下的位置吗?

谢谢!

【问题讨论】:

    标签: ios iphone geolocation location


    【解决方案1】:

    不知道有没有开箱即用的方法可以直接使用。但在我的应用程序中,我使用了一种解决方法。我只是像这样将最近的用户位置保存到 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"]];
    }
    

    希望能给你一些解决这个问题的想法。

    【讨论】:

      猜你喜欢
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多