繁体   English   中英

获取当前位置时出现问题

[英]Issue in getting current location

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

问题是用户取消授权应用程序使用位置服务,然后再次授权 在这种情况下,在此之后,如果我试图让位置它给nil虽然它叫

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

状态为3委托方法,即kCLAuthorizationStatusAuthorized

获取当前位置的代码:

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)
    {

    }
}

有什么想法吗?

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

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

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

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

苹果文档CLLocationManager

通常这意味着您要打电话

[self.locationManager startUpdatingLocation]

但您也可以使用

[self.locationManager startMonitoringSignificantLocationChanges]

如果将委托设置为nil,将不会获得有关授权状态更改的任何更新,不是吗?

self.locationManager.delegate = nil;

我认为您应该保留该委托以便接收授权状态更新,然后调用startUpdatingLocation方法以获得当前位置。

- (void)startUpdatingLocation

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM