Men的博客

欢迎光临!

0%

CoreLocation


CLLocationManager 定位管理者
CLLocation 代表位置(经度/纬度/高度/速度/路线等)
CLHeading 代表移动方向
CLRegion 代表一个区域

CLCircularRegion 圆形区域
CLBeaconRegion 蓝牙信号区域

返回定位服务是否可用
[CLLocationManager locationServicesEnabled];
返回延迟定位更新是否可用
[CLLocationManager deferredLocationUpdatesAvailable];
返回重大位置改变监听是否可用
[CLLocationManager significantLocationChangeMonitoringAvailable];
返回是否支持磁力计算方向
[CLLocationManager headingAvailable];
返回蓝牙信号范围服务是否可用
[CLLocationManager isRangingAvailable];


设置是否可以暂停定位来节省电池电量, 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; 设备移动速度 单位是米/秒, 适用于行车速度而不太适用于不行
/*
可以设置模拟器模拟速度
bicycle ride 骑车移动
run 跑动
freeway drive 高速公路驾车
*/


// CLAuthorizationStatus
用户从未选择过权限
kCLAuthorizationStatusNotDetermined
无法使用定位服务,该状态用户无法改变
kCLAuthorizationStatusRestricted
用户拒绝该应用使用定位服务,或是定位服务总开关处于关闭状态
kCLAuthorizationStatusDenied
已经授权(废弃)
kCLAuthorizationStatusAuthorized
用户允许该程序无论何时都可以使用地理信息
kCLAuthorizationStatusAuthorizedAlways
用户同意程序在可见时使用地理位置
kCLAuthorizationStatusAuthorizedWhenInUse


// 计算两个位置之间的距离, 单位是米
[newLocation distanceFromLocation:self.prevLocation];


获取方向信息不会提示用户(不需要授权), 因为不会泄露隐私
// [self.mgr startUpdatingHeading];

magneticHeading 设备与磁北的相对角度
trueHeading 设置与真北的相对角度, 必须和定位一起使用, iOS需要设置的位置来计算真北
真北始终指向地理北极点
磁北对应随着时间变化的地球磁场北极
@interface ViewController ()
{
//创建对象
CLLocationManager *_manager;
}

//获取当前经度
_manager =[[CLLocationManager alloc]init];
//设置代理
_manager.delegate=self;

开始定位
[self startLocation];
#pragma mark - 开始定位
-(void)startLocation
{
//判断定位服务是否可用
if (![CLLocationManager locationServicesEnabled]) {
NSLog(@”定位服务不可用”);

}

//设置精确度
_manager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
//距离变化敏感程度
_manager.distanceFilter=1000;
//开始定位
[_manager startUpdatingLocation];
}
#pragma mark - 协议提供的代理方法,当位置更新的时候执行
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//地址:经纬度

//获取地址对象(包含经纬度)
CLLocation *location=manager.location;
//获取当前经纬度
CLLocationCoordinate2D coordinate=location.coordinate;
//输出经纬度
NSLog(@”long=%f lan=%f”,coordinate.longitude,coordinate.latitude);

}

方向服务
// [self testHeading];
#pragma mark - 方向服务
-(void)testHeading
{
if (![CLLocationManager headingAvailable]) {
NSLog(@”方向服务不可用”);
return;
}
[_manager startUpdatingHeading];
}
#pragma mark - 代理方法,当方向更新的时候开始执行
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{

NSLog(@”磁极方向= %f”,newHeading.magneticHeading);
NSLog(@”地理方向 = %f”,newHeading.trueHeading);
}

地址编码和地址反编码
[self testGeoCoder];
#pragma mark - 地址编码和地址反编码
-(void)testGeoCoder
{
//知道地址,获取经纬度===地址编码
CLGeocoder *geocoCoder=[[CLGeocoder alloc]init];
[geocoCoder geocodeAddressString:@”兴美生活广场” completionHandler:^(NSArray *placemarks, NSError *error) {
//返回placemarks
for (CLPlacemark *mark in placemarks)
{
NSLog(@”mark=%@”,mark );
}
}];
//地址反编码,知道经纬度,获取地址,
CLLocation *location=[[CLLocation alloc]initWithLatitude:40.03708400 longitude:116.37059500];

CLGeocoder *reverseGeocoCoder=[[CLGeocoder alloc]init];
[reverseGeocoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark *mark in placemarks)
{
NSLog(@”地址反编码=%@”,mark );

}
}];

}