[英]Checking for iOS Location Services
我有一個帶有地圖和按鈕的視圖(比如地圖應用程序一次),允許用戶在地圖上居中和縮放他當前的位置。 如果我不能使用locationServicesEnabled方法(總是返回YES),我應該創建一個BOOL屬性來檢查是否調用了didFailWithError方法並知道我是否可以調用button方法?
謝謝閱讀。
編輯:
此代碼對我不起作用。 我正在使用模擬器。 在詢問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];
}
}
在“首選項”中,您有兩個選項可禁用位置服務。 第一個選項是全局開關,用於禁用所有應用程序“[CLLocationManager locationServicesEnabled]”的位置服務。 第二個選項允許您禁用某些應用的位置服務,但不能禁用所有應用。
要檢查全局是否已禁用以及是否已為您的應用禁用,請執行以下操作:
if([CLLocationManager locationServicesEnabled] &&
[CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
...
}
- (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;
}
}
“locationServicesEnabled”檢查用戶是否在“首選項”中啟用了“位置服務”。 您的MapView可能已經檢查了此值,如果位置服務不可用,則不應將任何值設置為“self.mapView.userLocation”。 這個問題可能會給你更多信息。
我也遇到了這個問題,仍然在尋找答案。
注意,authorizationStatus需要iOS4.2 +和+(BOOL)locationServicesEnabled需要iOS4.0 ...對於以前的iOS版本,它是 - (BOOL)locationServicesEnabled ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.