2014-04-01 37 views
0

我想获取设备的当前位置。代码正常工作正常。如果用户未更改位置服务的应用程序授权状态,则会给出位置。我也能够检查用户是否拒绝了位置服务的许可。获取当前位置的问题

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

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

委托方法与状态3kCLAuthorizationStatusAuthorized

代码获取当前位置:

CLLocation * location = self.locationManager.location; 

getter方法:

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

    } 
} 

对此有何想法?

+0

在上面你将locationManager.delegate设置为nil,如果授权被撤销......你是否曾经将它重新设置为适当的类? – Volker

+1

只是注意到你还将locationManager设置为零... – Volker

+1

根据您在代码中的意见,您可以在某处重新创建'locationManager'对象 - 您确定在设置'self.locationServiceDisabled = false'时触发了这个对象吗? – Paulw11

回答

0

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

在苹果文档中指出有关的LocationManager的location属性:

此属性的值是零,如果没有位置的数据已经去过检索 。

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

Apple Docs CLLocationManager

通常,这意味着你要调用

[self.locationManager startUpdatingLocation]

,但你也可以使用

[self.locationManager startMonitoringSignificantLocationChanges]

+0

我希望你在这里添加你评论的全文。这会让这个答案好多了。谢谢。 – Geek

0

如果委托设为零,你将得不到关于授权状态更新的更新,不是吗?

self.locationManager.delegate = nil; 

,我认为你应该保持的委托,以获得授权状态更新,然后调用startUpdatingLocation方法,以获得当前位置。

- (void)startUpdatingLocation 
+0

如果您关心仔细查看我的代码,那么请清楚,在getter方法中,我再次设置委托。 – Geek