--------------------------------------------------------
CLLocationManager 定位管理者
CLLocation 代表位置(经度/纬度/高度/速度/路线等)
CLHeading 代表移动方向
CLRegion 代表一个区域
>CLCircularRegion 圆形区域
>CLBeaconRegion 蓝牙信号区域
// 返回定位服务是否可用
[CLLocationManager locationServicesEnabled];
// 后的系版本号
[UIDevice currentDevice].systemVersion.floatValue >= 8.0
// 跳转到手机设置页面
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
// 配置info.plist
NSLocationWhenInUseUsageDescription // iOS 8.0 使用前台定位服务
NSLocationAlwaysUsageDescription // iOS 8.0 使用前后台都会定位
// Title:@"定位系统不可用" message:@"请进入设置页面 > 隐私 > 定位服务 > 打开定位服务 > 选择当前应用 > 勾上'使用应用期间'.设置完毕后请重新启动App运行."
--------------------------------------------------------
// 设置是否可以暂停定位来节省电池电量, YES不需要定位数据时自动暂停定位
mgr.pausesLocationUpdatesAutomatically
--------------------------------------------------------
// 每隔多少米定位一次, 只有水平方向超过该值时才会重新定位
mgr.distanceFilter = 100;
--------------------------------------------------------
定位精确度
// mgr.desiredAccuracy;
kCLDistanceFilterNone;
kCLLocationAccuracyBestForNavigation // 导航级最佳精准
kCLLocationAccuracyBest; // 最佳精准
kCLLocationAccuracyNearestTenMeters; // 10米误差
kCLLocationAccuracyHundredMeters; // 百米误差
kCLLocationAccuracyKilometer; // 千米误差
kCLLocationAccuracyThreeKilometers; // 3千米误差
--------------------------------------------------------
定位数据的用途
// mgr.activityType;
CLActivityTypeOther // 作为普通用途
CLActivityTypeAutomotiveNavigation // 作为车辆导航
CLActivityTypeFitness // 作为步行导航
CLActivityTypeOtherNavigation // 作为其它导航
--------------------------------------------------------
// CLLocation
location.coordinate; // 坐标, 包含经纬度
location.altitude; // 设备海拔高度 单位是米
location.course; // 设置前进方向 0表示北 90东 180南 270西
location.horizontalAccuracy; // 水平精准度
location.verticalAccuracy; // 垂直精准度
location.timestamp; // 定位信息返回的时间
location.speed; // 设备移动速度 单位是米/秒, 适用于行车速度而不太适用于不行
--------------------------------------------------------
CLAuthorizationStatus
// 用户尚未决定授权权限
kCLAuthorizationStatusNotDetermined
// 无法使用定位服务,该状态用户无法改变
kCLAuthorizationStatusRestricted
// 用户拒绝该应用使用定位服务,或是定位服务总开关处于关闭状态
kCLAuthorizationStatusDenied
// 已经授权(废弃)
kCLAuthorizationStatusAuthorized
// 用户允许该程序无论何时都可以使用地理信息
kCLAuthorizationStatusAuthorizedAlways
// 用户同意程序在可见时使用地理位置
kCLAuthorizationStatusAuthorizedWhenInUse
--------------------------------------------------------
注意:iOS9.0 可以单次请求用户位置
- (void)requestLocation
// 成功调用,locations位置数组,元素按照时间排序
-(void)locationManager:(nonnull CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations
// 失败调用
-(void)locationManager:(nonnull CLLocationManager *)manager didFailWithError:(nonnull NSError *)error
requestLocation 作用:
// 按照定位精确度从低到高进行排序,逐个进行定位。如果获取到的位置不是精确度最高的那个,也会在定位超时后,通过代理告诉外界(必须实现代理的-locationManager:didFailWithError:方法)
不能与startUpdatingLocation方法同时使用
--------------------------------------------------------
// 计算两个位置之间的距离, 单位是米
[newLocation distanceFromLocation:self.prevLocation];
--------------------------------------------------------
获取方向信息不会提示用户(不需要授权), 因为不会泄露隐私
// [self.mgr startUpdatingHeading];
magneticHeading // 设备与磁北的相对角度
trueHeading // 设置与真北的相对角度, 必须和定位一起使用, iOS需要位置来计算真北
--------------------------------------------------------
// 错误:使用CoreLocation获取地理位置信息,报错
Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)"
解决方法:
1.确定模拟器(手机)已经联网并且允许程序获取地理位置
2.重置地理位置服务或者网络服务
PS:如果是模拟器就果断直接重置模拟器吧 IOS Simulator - Reset Content and Settings...
-----------------------------------
#pragma mark - 导航 MKDirections / MKDirectionsRequest
Demo: 根据起点与终点坐标, 向Apple服务器请求路线规划信息, 再绘制路线
1. 获取起点与终点的地理坐标
2. 创建 MKDirectionRequest , 配置起点与终点的 MapItem
3. 使用 MKDirection 进行路线规划请求 ( MKRoute, MKRouteStep )
calculateDirectionsWithCompletionHandler
4. 将获取到的路线信息绘制到地图上. ( MKPolyLineRender )
// 渲染方法
[self.mapView addOverlay:toute.polyline];
// 设置颜色的代理方法
// 渲染路线
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
// 设置颜色
MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
renderer.lineWidth = 2.0;
renderer.strokeColor = [UIColor blueColor];
return renderer;
}