检查 iOS 定位服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4318708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:11:32  来源:igfitidea点击:

Checking for iOS Location Services

objective-cioscllocationmanager

提问by Daniel García Baena

I have a view with a map and a button (like the Maps app once) that allows the user to center and zoom his current location on the map. If I can not use the locationServicesEnabled method (always returns YES), should I create a BOOL attribute to check if the didFailWithError method is called and know if I can call the button method?

我有一个带有地图和按钮的视图(就像一次地图应用程序),允许用户在地图上居中和缩放他的当前位置。如果我不能使用 locationServicesEnabled 方法(总是返回 YES),我是否应该创建一个 BOOL 属性来检查是否调用了 didFailWithError 方法并知道我是否可以调用按钮方法?

Thanks for reading.

谢谢阅读。

Edited:

编辑:

This code does not work for me. I am using the simulator. I am always getting YES when asking locationServicesEnabled.

这段代码对我不起作用。我正在使用模拟器。在询问 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];     
    }
}

回答by Robin

In Preferences you have two options to disable the location services. The first option is a global switch to disable the location service for all apps "[CLLocationManager locationServicesEnabled]". The second option let you disable the location service for some apps but not for all apps.

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

To check if its disabled globally and if its disabled for your app use following:

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

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

回答by Nippysaurus

"locationServicesEnabled" checks if the user has enabled Location Services in Preferences. Your MapView probably checks this value already and should not set any values to "self.mapView.userLocation" if Location Services are not available. This SO question might give you some more info.

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

回答by lin zheng

I run into this problem too and still be finding the answer.

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

take care that authorizationStatus requires iOS4.2+ and + (BOOL)locationServicesEnabled requires iOS4.0... And for previous iOS versions, it is - (BOOL)locationServicesEnabled...

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

回答by P.J.Radadiya

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