iOS 8 Mapview当前位置不会激发

时间:2014-09-18 11:21:42

标签: objective-c iphone mkmapview

MKMapview当前用户位置未在iOS-8,之前iOS-7& iOS-6工作正常。

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

在此行中自动调用用户当前位置委托方法

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

但不会在iOS-8中触发。

3 个答案:

答案 0 :(得分: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。

将值保留为空以使用默认消息,或者您可以通过输入值来自定义自己的值。

答案 1 :(得分: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

enter image description here

答案 2 :(得分:0)

在iOS 8 Mapview当前位置使用

启动

您可以在.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 *)位置 {

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

}

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