【问题标题】:Keep monitoring user's location whenever it changes its position每当用户改变位置时,持续监控用户的位置
【发布时间】:2015-03-02 11:27:14
【问题描述】:

我是定位服务的新手。我使用了startMonitoringSignificantLocationChanges(void)locationManager:(CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 方法,但是当我更改位置值时它没有被调用。每当用户更改其位置时,我都想获取位置值。我应该如何实现这一点?请帮我解决。提前致谢。

【问题讨论】:

  • 你分配了 CLLocationManagerDelegate 了吗?
  • @DheerajSingh 是的,我使用过 myLocationManager.delegate = self;并分配委托给它。
  • 你用的是ios8吗?你能显示你的代码吗
  • @DheerajSingh 没有 ios 7,我知道对于 ios 8 我们需要使用“requestWhenInUseAuthorization”,我已经做到了。

标签: ios core-location cllocation


【解决方案1】:

您需要实现“didUpdateLocations”委托方法。这是示例

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"locations : %@",locations.description);

   CLLocation *currentLocation = [locations lastObject];
    NSLog(@"current location : %f %f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);



}

【讨论】:

  • 请找到我更新的问题,是的,我也尝试过这种方法,但它不起作用。
  • didUpdateToLocation 方法已弃用。如果实现了 locationManager:didUpdateLocations: 则不会调用该方法。一旦检查它
  • 我正在使用您的 updateLocation 方法,然后它没有被调用。为什么会这样?
【解决方案2】:

在 iOS 8 中,您需要做两件额外的事情才能让定位工作: 1. 将以下一个或两个键添加到您的 Info.plist 文件中:

NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription

  1. 接下来您需要为相应的定位方法,WhenInUse 或 Background 请求授权。使用以下调用之一:

    [self.locationManager requestWhenInUseAuthorization];

    [self.locationManager requestAlwaysAuthorization];

有关详细信息,请参阅以下链接。 http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/

【讨论】:

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