获取当前位置时出现问题

极客

我想获取设备的当前位置。代码正常工作。如果用户未更改位置服务的应用授权状态,则会提供位置信息。我还可以检查用户是否拒绝了位置服务的许可。

问题是用户取消授权应用程序使用位置服务,然后再次授权在这种情况下,在此之后,如果我尝试获取位置,nil尽管它调用了

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

委托方法与状态3kCLAuthorizationStatusAuthorized

获取当前位置的代码:

CLLocation * location = self.locationManager.location;

吸气方法:

- (CLLocationManager *)locationManager
{
    if (!locationManager)
    {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
    }

    return locationManager;
}

CLLocationManager委托方法:

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    DLog(@"Location authorization changed : %d", status);

    // If user has denied permission for location service
    if (status == kCLAuthorizationStatusDenied)
    {
        DLog(@"Location service denied.");

        // If authorization status changed method is already called, then SDK will not call again on same object.
        // Thus, set CLLocationManager object to nil so that next time we try to get location, it will create a new object,
        // and that will send message about authorization status changed.
        self.locationManager.delegate = nil;
        self.locationManager = nil;
    }
    else if (status == kCLAuthorizationStatusNotDetermined)
    {
        // If authorization status changed method is already called, then SDK will not call again on same object.
        // Thus, set CLLocationManager object to nil so that next time we try to get location, it will create a new object,
        // and that will send message about authorization status changed.
        self.locationManager.delegate = nil;
        self.locationManager = nil;
    }
    else if (status == kCLAuthorizationStatusAuthorized)
    {

    }
}

关于这个有什么想法吗?

尼尔斯·齐恩(Nils Ziehn)

self.locationManager.locationnil因为您从未开始更新位置。

在Apple文档中,它声明location了locationManager属性:

如果从未检索到任何位置数据,则此属性的值为nil。

因此,您需要以某种方式更新iPhone的位置!

苹果文档CLLocationManager

通常这意味着您要打电话

[self.locationManager startUpdatingLocation]

但您也可以使用

[self.locationManager startMonitoringSignificantLocationChanges]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章