繁体   English   中英

iOS 8 Mapview当前位置不会激发

[英]iOS 8 Mapview Current Location not fire

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

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

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

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

但它不会在iOS-8激发。

在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。

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

在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

在此输入图像描述

在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 *)locations {

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

}

我使用这个我的应用程序得到完美的当前位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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