繁体   English   中英

检查iOS位置服务

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM