前段时间,公司开发的App用到了地图和定位,所以记录一下,作为回顾总结。
    对于地图和定位,苹果公司提供给了两个框架:
  • MapKit:用于地图展示
  • Core
    Location :用于地理定位

CoreLocation的使用:

1、导入头文件
     #import<CoreLocation/CoreLocation.h>
  • CoreLocation框架使用须知
  • CoreLocation框架中所有数据类型的前缀都是CL
  • CoreLocation中使用CLLocationManager对象来做用户定位
     
2、CLLocationManager
      CoreLocation 提供了 CLLocationManager类,来管理定位的相关操作:如开始定位、停止定位,申请授权(这是iOS8.0之后提供的方法)等;
    
     使用步骤:     
     1、实例化一个CLLocationManager对象;并用一个strong指向该对象(这样才会一致存在,供我们使用);
     2、开始用户定位
     - (void)startUpdatingLocation;
     (- (void)stopUpdatingLocation;停止定位)
  3、设置代理
  4、请求授权
     授权方式有两个:
         看单词字面意思就知道一个是后台一直运行,一个直程序运行时;
          requestWhenInUseAuthorization
          requestAlwaysAuthorization
     5、实现相关代理方法      

    特别注意:在使用授权时,需要配置info.plist
           >设置方法 requestWhenInUseAuthorization
 配置plist文件NSLocationWhenInUseUsageDescription
      >设置方法requestAlwaysAuthorization 
   配置plist文件NSLocationAlwaysUsageDescription 
     

代码如下:

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self location];
}
-(void)location{ self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self; if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
NSLog(@"%@",locations);
//[self.locationManager stopUpdatingLocation];
}

小结:
  • 当调用了startUpdatingLocation方法后,就开始不断地定位用户的位置,中途会频繁地调用代理的下面方法
  • - (void)locationManager:(CLLocationManager*)managerdidUpdateLocations:(NSArray*)locations;
  • locations参数里面装着CLLocation对象
   
   当然,可以跟久需要设置CLLocationManager的其它属性已满足需求
  • @property(assign,nonatomic)CLLocationDistancedistanceFilter;
  • 每隔多少米定位一次

  • @property(assign,nonatomic)CLLocationAccuracydesiredAccuracy;
  • 定位精确度(越精确就越耗电)
 
   
3、CLLocation
   CLLocation用来表示某个位置的地理信息,比如经纬度、海拔等等
  • @property(readonly,nonatomic)CLLocationCoordinate2D coordinate;
  • 经纬度
  • @property(readonly,nonatomic)CLLocationDistance altitude;
  • 海拔
  • @property(readonly,nonatomic)CLLocationDirection course;
  • 路线,航向(取值范围是0.0°~359.9°,0.0°代表真北方向)
  • @property(readonly,nonatomic)CLLocationSpeed speed;
  • 行走速度(单位是m/s)
  • 用- (CLLocationDistance)distanceFromLocation:(constCLLocation*)
    location方法可以计算2个位置之间的距离

     eg:
     -(void)compare{
         CLLocation*locationFirst
= [[CLLocationalloc]initWithLatitude:80longitude:100];
         CLLocation*locationSecond
= [[CLLocationalloc]initWithLatitude:60longitude:100];
        CLLocationDistancedistance
= [locationFirst
distanceFromLocation:locationSecond];
         NSLog(@"distance:%f
km ",distance/1000);
     }
     在测试的时候可以修改模拟器的坐标,在——Debug-Location-Custom Location


4、CLLocationCoordinate2D
     CLLocationCoordinate2D是一个用来表示经纬度的结构体,定义如下
     typedefstruct{
       CLLocationDegrees latitude;//纬度
       CLLocationDegrees longitude;//经度
     } CLLocationCoordinate2D;
           一般用CLLocationCoordinate2DMake 函数来创建CLLocationCoordinate2D

5、说点隐私问题:
     在iOS6.0之后,苹果在保护用户隐私方面做了很大的加强,以下操作都必须经过用户批准授权;
  • 用户的位置
  • 用户的通讯录、日历、相机、相册等
     当想访问用户的隐私信息时,系统会自动弹出一个对话框让用户授权;
     一旦用户选择了“Don’tAllow”,意味着你的应用以后就无法使用定位功能
     为了严谨起见,最好在使用定位功能之前判断当前应用的定位功能是否可用
     CLLocationManager有个类方法可以判断当前应用的定位功能是否可用
     + (BOOL)locationServicesEnabled;
6、CLGeocoder (地理编码,反地理编码)
     
  • 使用CLGeocoder可以完成“地理编码”和“反地理编码”
  • 地理编码:根据给定的地名,获得具体的位置信息(比如经纬度、地址的全称等)
  • 反地理编码:根据给定的经纬度,获得具体的位置信息

  • 地理编码方法
  • - (void)geocodeAddressString:(NSString*)addressStringcompletionHandler:(CLGeocodeCompletionHandler)completionHandler;
  • 反地理编码方法
     - (void)reverseGeocodeLocation:(CLLocation*)locationcompletionHandler:(CLGeocodeCompletionHandler)completionHandler
     
     完成后的回调函数CLGeocodeCompletionHandler,会返回一个地标数组
  • typedefvoid(^CLGeocodeCompletionHandler)(NSArray*placemarks,NSError*error);
     
     CLPlacemark的字面意思是地标,封装详细的地址位置信息
  • @property(nonatomic,readonly)CLLocation*location;
  • 地理位置
  • @property(nonatomic,readonly)CLRegion*region;
  • 区域
  • @property(nonatomic,readonly)NSDictionary*addressDictionary;
  • 详细的地址信息
  • @property(nonatomic,readonly)NSString*name;
  • 地址名称
  • @property(nonatomic,readonly)NSString*locality;
  • 城市
     ...
     CLPlacemark CLLocation的关系;
          从包含关系上就可以看出,一个地标除了包含CLLocation外,还有一些其它信息;

解码和反解码的一个非常简单的demo在我的github上,有兴趣的可以去看看。

https://github.com/yscGit/MapCodeUncode

05-11 22:43