2010-11-30 60 views
20

我有一個地圖和一個按鈕(如地圖應用程序一次)的視圖,允許用戶居中放大地圖上的當前位置。如果我不能使用locationServicesEnabled方法(總是返回YES),我應該創建一個BOOL屬性來檢查didFailWithError方法是否被調用,並知道我是否可以調用按鈕方法?檢查iOS定位服務

感謝您的閱讀。

編輯:

此代碼不會爲我工作。我正在使用模擬器。詢問locationServicesEnabled時,我總是收到YES。

// Gets the user present location. 
- (IBAction)locateUser:(id)sender { 

    if([CLLocationManager locationServicesEnabled]) { 

     CLLocationCoordinate2D coordinate; 

     coordinate.latitude = self.mapView.userLocation.location.coordinate.latitude; 
     coordinate.longitude = self.mapView.userLocation.location.coordinate.longitude; 

     [self zoomCoordinate:coordinate]; 
    } else { 
     [[[[UIAlertView alloc] initWithTitle:@"Warning." message:@"Location services are disabled." 
            delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease] show];  
    } 
} 
+1

我有發佈**優化位置權限檢查**在這篇文章,這一定會幫助你http://stackoverflow.com/questions/15153074/checking-location-service-permission-on-ios/35982887#35982887 – swiftBoy 2016-03-14 08:46:10

回答

99

在首選項中,您有兩個選項來禁用位置服務。第一個選項是全局開關,用於禁用所有應用程序的位置服務「[CLLocationManager locationServicesEnabled]」。第二個選項可以讓您爲某些應用禁用位置服務,但不是針對所有應用。

要如果你的應用程序使用禁用以下檢查其全球範圍的殘疾人和:

if([CLLocationManager locationServicesEnabled] && 
    [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied) 
{ 
... 
} 
+0

因爲只是檢查[CLLocationManager locationServicesEnabled]是不夠的,所以我很苦惱。謝謝! – fellowworldcitizen 2013-04-02 18:16:40

1

「locationServicesEnabled」檢查用戶是否在首選項中啓用了位置服務。您的MapView可能已經檢查過這個值,如果定位服務不可用,不應該將任何值設置爲「self.mapView.userLocation」。 This SO question might give you some more info.

+0

我已編輯我的題。 – 2010-11-30 21:27:18

1

我遇到這個問題也是一樣,仍然可以找到答案。

要小心,authorizationStatus需要的iOS4.2 +和+(BOOL)locationServicesEnabled需要IOS4.0 ......而對於以前的IOS版本,它是 - (BOOL)locationServicesEnabled ...

1
- (BOOL) enableLocationServices 
{ 

    if ([CLLocationManager locationServicesEnabled]) 
    { 
     self.locationManager.distanceFilter = 10; 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
     [self.locationManager startUpdatingLocation]; 
     [self.mapview setUserTrackingMode:MKUserTrackingModeFollow animated:YES]; 
     return YES; 
    } 
    else 
    { 
     return NO; 
    } 
}