检查iOS位置服务

时间:2010-11-30 21:12:21

标签: objective-c ios cllocationmanager

我有一个地图和一个按钮的视图(比如地图应用程序一次),允许用户在地图上居中和缩放他当前的位置。如果我不能使用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];     
    }
}

4 个答案:

答案 0 :(得分:99)

在“首选项”中,您有两个选项可禁用位置服务。第一个选项是全局开关,用于禁用所有应用程序“[CLLocationManager locationServicesEnabled]”的位置服务。第二个选项允许您禁用某些应用的位置服务,但不能禁用所有应用。

要检查全局是否已禁用,以及是否为您的应用禁用了以下内容:

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

答案 1 :(得分:1)

“locationServicesEnabled”检查用户是否在“首选项”中启用了“位置服务”。您的MapView可能已经检查了此值,如果位置服务不可用,则不应将任何值设置为“self.mapView.userLocation”。 This SO question might give you some more info.

答案 2 :(得分:1)

我也遇到了这个问题,仍然在寻找答案。

注意authorizationStatus需要iOS4.2 +和+(BOOL)locationServicesEnabled需要iOS4.0 ...对于以前的iOS版本,它是 - (BOOL)locationServicesEnabled ...

答案 3 :(得分: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;
    }
}