检查iOS位置服务

我有一个地图和一个button的视图(如地图应用程序一次),允许用户居中和放大他在地图上的当前位置。 如果我不能使用locationServicesEnabled方法(总是返回YES),我应该创build一个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) { ... } 

“locationServicesEnabled”检查用户是否在首选项中启用了位置服务。 您的MapView可能已经检查了这个值,如果定位服务不可用,不应该将任何值设置为“self.mapView.userLocation”。 这个问题可能会给你更多的信息。

我也遇到这个问题,仍然在find答案。

请注意,authorizationStatus需要iOS4.2 +和+(布尔)locationServicesEnabled需要iOS4.0 …而对于以前的iOS版本,它是 – (布尔)locationServicesEnabled …

 - (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; } }