检查 iOS 定位服务

发布于 2024-10-05 05:59:55 字数 929 浏览 0 评论 0原文

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

感谢您的阅读。

已编辑:

此代码对我不起作用。我正在使用模拟器。当询问 locationServicesEnabled 时,我总是得到“是”。

// 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];     
    }
}

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?

Thanks for reading.

Edited:

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

// 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];     
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

携君以终年 2024-10-12 05:59:55

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

要检查其是否全局禁用以及是否对您的应用程序禁用,请使用以下命令:

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

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.

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

if([CLLocationManager locationServicesEnabled] && 
   [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
...
}
枕梦 2024-10-12 05:59:55

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

"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.

唯憾梦倾城 2024-10-12 05:59:55

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

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

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...

已下线请稍等 2024-10-12 05:59:55
- (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;
    }
}
- (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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文