iOS 8地图视图无法获取当前位置

5

iOS-8MKMapview当前用户位置没有触发,之前的iOS-7iOS-6都能正常工作。

     self.mapView.delegate = self;
     self.mapView.showsUserLocation =YES; 

在这一行中,自动调用用户当前位置委托方法。
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
}

但是它在上无法启动。


可能是重复的问题:iOS 8:位置服务无法工作 - Adrian Toman
3个回答

6
在iOS8中,获取用户位置之前需要请求用户授权。有两种请求方式:
- -[CLLocationManager requestWhenInUseAuthorization]:只允许在应用程序启动时获取用户位置。 - -[CLLocationManager requestAlwaysAuthorization]:即使应用程序在后台运行也可以获取用户位置。
您可以根据需求选择其中一种请求方式。例如,在开始更新位置信息之前,可以这样做:
// ask for authorization
CLLocationManager * locationManager = [[CLLocationManager alloc] init];
// check before requesting, otherwise it might crash in older version
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { 

     [locationManager requestWhenInUseAuthorization];

}

此外,不要忘记添加两个键。
NSLocationWhenInUseUsageDescription

并且

NSLocationAlwaysUsageDescription

将内容添加到您的info.plist文件中。
如果将值留空,则使用默认消息,或者您可以通过输入值来自定义自己的消息。

3

iOS 8需要以下授权:

    NSString * osVersion = [[UIDevice currentDevice] systemVersion];
    if ([osVersion floatValue]>= 8.0 ) {
    [_CLLocationManager requestAlwaysAuthorization]; //Requests permission to use location services whenever the app is running. 
// [_CLLocationManager requestWhenInUseAuthorization]; //Requests permission to use location services while the app is in the foreground. 
    }
    [_CLLocationManager startUpdatingLocation];

您需要在plist中添加两个键

1.NSLocationAlwaysUsageDescription

2.NSLocationWhenInUseUsageDescription

输入图像描述


0

iOS 8 地图视图可以使用当前位置触发

您可以在.plist文件中进行设置

<key>NSLocationWhenInUseUsageDescription</key>
<string>Your app name Or Other string</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Your app name Or Other string</string>

在你的代码中,你只需要实现这个:

CLLocationManager * locationmanager = [CLLocationManager new];

locationmanager.delegate = self;
locationmanager.distanceFilter = kCLDistanceFilterNone;
locationmanager.desiredAccuracy = kCLLocationAccuracyBest;
[locationmanager startUpdatingLocation];    
[locationmanager requestAlwaysAuthorization];

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { }

location = [locations objectAtIndex:0];
[locationmanager stopUpdatingLocation];

}

我正在使用这个应用程序来获取完美的当前位置。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接