简体   繁体   English

获取当前位置时出现问题

[英]Issue in getting current location

I want to get current location of device. 我想获取设备的当前位置。 Code works fine normally. 代码正常工作。 It gives location if user has not changed app authorization status for location service. 如果用户未更改位置服务的应用授权状态,则会提供位置信息。 I am also able to check if user has denied permission for location service. 我还能够检查用户是否拒绝了位置服务的许可。

Issue is when user deauthorizes the app to use location service and then authorizes again . 问题是用户取消授权应用程序使用位置服务,然后再次授权 In this case, after this if I try to get location it gives nil though it called 在这种情况下,在此之后,如果我试图让位置它给nil虽然它叫

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

delegate method with status 3 ie kCLAuthorizationStatusAuthorized . 状态为3委托方法,即kCLAuthorizationStatusAuthorized

Code to get current location : 获取当前位置的代码:

CLLocation * location = self.locationManager.location;

Getter method : 吸气方法:

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

    return locationManager;
}

CLLocationManager delegate method : 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)
    {

    }
}

Any idea about this? 有什么想法吗?

self.locationManager.location is nil since you never started updating the location. self.locationManager.locationnil因为您从未开始更新位置。

In the apple docs it is stated about the location property of the locationManager: 在Apple文档中,它声明了location属性:

The value of this property is nil if no location data has ever been retrieved. 如果从未检索到任何位置数据,则此属性的值为nil。

For this reason you need to somehow update your iPhones location! 因此,您需要以某种方式更新iPhone的位置!

Apple Docs CLLocationManager 苹果文档CLLocationManager

Usually this means you want to call 通常这意味着您要打电话

[self.locationManager startUpdatingLocation]

but you can also use 但您也可以使用

[self.locationManager startMonitoringSignificantLocationChanges]

If you set the delegate to nil you will get no updates about authorization status changes, isn't it? 如果将委托设置为nil,将不会获得有关授权状态更改的任何更新,不是吗?

self.locationManager.delegate = nil;

I think you should keep the delegate in order to receive authorization status updates and then call the startUpdatingLocation method in order to get the current location. 我认为您应该保留该委托以便接收授权状态更新,然后调用startUpdatingLocation方法以获得当前位置。

- (void)startUpdatingLocation

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

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