簡體   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