三、进行GEO编码的工具类CLGeocoder
如前所述,使用CLLocationManager获取到的位置信息是CLLocation对象,这个对象封装了经纬度等基础信息,但是在实际开发中,我们往往需要获取到的是位置的更多实际信息,比如国家,省份,城市等等,GEO编码的作用就是通过经纬度信息发起请求,获取现实意义的更多数据。CLGeocoder类解析如下:
//是否正在进行GEO编码
@property (nonatomic, readonly, getter=isGeocoding) BOOL geocoding;
//反地理位置信息编码
/*
CLGeocodeCompletionHandler会传回一组标志建筑物
*/
- (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler;
//作用同上 local用来设置区域 可以通过这个参数控制返回的语言
- (void)reverseGeocodeLocation:(CLLocation *)location preferredLocale:(nullable NSLocale *)locale completionHandler:(CLGeocodeCompletionHandler)completionHandler;
//下面这些方法通常与 AddressBook配合使用
//将地址信息字典进行编码 这个方法与AddressBook框架配合使用 AddressBook框架中定义这个字典
- (void)geocodeAddressDictionary:(NSDictionary *)addressDictionary completionHandler:(CLGeocodeCompletionHandler)completionHandler;
- (void)geocodeAddressString:(NSString *)addressString completionHandler:(CLGeocodeCompletionHandler)completionHandler;
- (void)geocodeAddressString:(NSString *)addressString inRegion:(nullable CLRegion *)region completionHandler:(CLGeocodeCompletionHandler)completionHandler;
- (void)geocodeAddressString:(NSString *)addressString inRegion:(nullable CLRegion *)region preferredLocale:(nullable NSLocale *)locale completionHandler:(CLGeocodeCompletionHandler)completionHandler;
//对邮编地址进行GEO编码
- (void)geocodePostalAddress:(CNPostalAddress *)postalAddress completionHandler:(CLGeocodeCompletionHandler)completionHandler;
- (void)geocodePostalAddress:(CNPostalAddress *)postalAddress preferredLocale:(nullable NSLocale *)locale completionHandler:(CLGeocodeCompletionHandler)completionHandler;
//取消编码
- (void)cancelGeocode;
四、位置信息模型CLLocation相关类
首先在CoreLocation框架中,位置的经纬度是由CLLocationCoordinate2D结构体描述的,这个结构体定义如下:
struct CLLocationCoordinate2D {
CLLocationDegrees latitude; //精度
CLLocationDegrees longitude;//维度
};
使用下面的函数可以快速的检查和创建CLLocationCoordinate2D对象:
//检查经纬度对象是否有效
BOOL CLLocationCoordinate2DIsValid(CLLocationCoordinate2D coord);
//创建对象
CLLocationCoordinate2D CLLocationCoordinate2DMake(CLLocationDegrees latitude, CLLocationDegrees longitude);
CLFloor类是一个用来描述楼层信息的模型,并不一定精准,是基于地面的粗略计算,其中属性如下:
@interface CLFloor : NSObject <NSCopying, NSSecureCoding>
//楼层 地下室可能为负值
@property(readonly, nonatomic) NSInteger level;
@end
下面列举了CLLocation中封装的属性的方法:
//初始化方法
//通过经纬度初始化 CLLocationDegrees就是double类型
- (instancetype)initWithLatitude:(CLLocationDegrees)latitude
longitude:(CLLocationDegrees)longitude;
//初始化方法
/*
hAccuracy设置水平方向的精确度
verticalAccuracy 设置垂直方向的精确度
timestamp 设置时间戳
*/
- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate
altitude:(CLLocationDistance)altitude
horizontalAccuracy:(CLLocationAccuracy)hAccuracy
verticalAccuracy:(CLLocationAccuracy)vAccuracy
timestamp:(NSDate *)timestamp;
/*
course设置方向
speed 设置速度
*/
- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate
altitude:(CLLocationDistance)altitude
horizontalAccuracy:(CLLocationAccuracy)hAccuracy
verticalAccuracy:(CLLocationAccuracy)vAccuracy
course:(CLLocationDirection)course
speed:(CLLocationSpeed)speed
timestamp:(NSDate *)timestamp;
//获取位置对象的经纬度信息
@property(readonly, nonatomic) CLLocationCoordinate2D coordinate;
//获取海波高度
@property(readonly, nonatomic) CLLocationDistance altitude;
//水平方向精度
@property(readonly, nonatomic) CLLocationAccuracy horizontalAccuracy;
//垂直方向精度
@property(readonly, nonatomic) CLLocationAccuracy verticalAccuracy;
//方向值 0-360之间
@property(readonly, nonatomic) CLLocationDirection course;
//速度 单位m/s
@property(readonly, nonatomic) CLLocationSpeed speed;
//时间戳
@property(readonly, nonatomic, copy) NSDate *timestamp;
//楼层信息
@property(readonly, nonatomic, copy, nullable) CLFloor *floor;
//获取到某个位置之间的距离
- (CLLocationDistance)getDistanceFrom:(const CLLocation *)location;
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location;
五、航向信息模型CLHeading
当使用CLLocationManager进行航向信息的请求时,代理回调中会获取到CLHeading对象,这个对象封装了航向相关数据,解析如下:
//地磁方向0-360
@property(readonly, nonatomic) CLLocationDirection magneticHeading;
//地理方向0-360
@property(readonly, nonatomic) CLLocationDirection trueHeading;
//航向精度
@property(readonly, nonatomic) CLLocationDirection headingAccuracy;
//x轴测量的原始值
@property(readonly, nonatomic) CLHeadingComponentValue x;
//y轴测量的原始值
@property(readonly, nonatomic) CLHeadingComponentValue y;
//z轴测量的原始值
@property(readonly, nonatomic) CLHeadingComponentValue z;
//时间戳
@property(readonly, nonatomic, copy) NSDate *timestamp;