您的位置:首页 > 移动开发 > IOS开发

iOS8环境下地图定位需要注意的地方

2014-10-15 16:06 211 查看
1.iOS8的定位服务需要用户取申请系统的授权

即需要在plist文件中增加两个string类型的键值对:NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription。同时在开始定位之前必须调用申请授权的方法:

CLLocationManager *locationManager = [[CLLocationManager alloc]init];

locationManager.delegate = self;

[locationManager requestAlwaysAuthorization];

[locationManager
requestWhenInUseAuthorization];

locationManager.desiredAccuracy = kCLLocationAccuracyBest;

locationManager.distanceFilter = kCLDistanceFilterNone;

[locationManager startUpdatingLocation];

requestAlwaysAuthorization和requestWhenInUseAuthorization两个方法即是在申请系统的授权。

2.实机运行时记得要在系统设置中打开手机的定位服务,如果时模拟器的话除了打开模拟器设置中的定位服务外还需要在edit
schema 中设置模拟器的默认位置。

3.如果进入了失败的委托方法,可以根据error的code值从
CLError.h文件中查对应的原因
4.为稳妥起见,CLLocationManager的didChangeAuthorizationStatus方法务必实现,代码如下
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case
kCLAuthorizationStatusNotDetermined:
if ([locationManager
respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[locationManager
requestAlwaysAuthorization];
}
break;
default:
break;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: