Corelocation及地图控件学习笔记

时间:2022-03-03 06:09:41

Corelocation基本使用

在地图章节的学习中,首先要学的便是用户位置定位,因此我们首先要掌握Corelocation的使用。(在IOS8以前可以系统会直接请求授权,现在需要我们自己调用方式通知系统请求授权)

首先设置一个Corelocation属性并实现懒加载设置代理,此对象需要自己调用方法startUpdatingLocation及stopUpdatingLocation来开始和结束位置获取

 //定位管理者
@property (nonatomic , strong ) CLLocationManager *manager;
- (CLLocationManager *)manager{
if (_manager == nil) {
//创建CoreLocation管理者
_manager = [[CLLocationManager alloc] init];
7 _manager.delegate = self;
8 }
9 return _manager;
10 }

设置好定位管理者对象后就对其属性进行一些常用的属性配置

 //配置定位精度
/*
导航级别: kCLLocationAccuracyBestForNavigation;
最优: kCLLocationAccuracyBest;
精确到10米: kCLLocationAccuracyNearestTenMeters;
精确到100米: kCLLocationAccuracyHundredMeters;
精确到1000米: kCLLocationAccuracyKilometer;
精确到3000米: kCLLocationAccuracyThreeKilometers;
*/
self.manager.desiredAccuracy = kCLLocationAccuracyBest;
//设置超过范围后更新数据,如不设置数据会一直不间断更新
self.manager.distanceFilter = ;

若使用[self.manager requestAlwaysAuthorization]方法需要添加NSLocationAlwaysUsageDescription为key

 //判断系统IOS版本
if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
NSLog(@"IOS8");
//注意:IOS8后需要验证授权,在Info.plist文件还要加上NSLocationWhenInUseUsageDescription这个key,Value可以为空,并调用此方法
[self.manager requestWhenInUseAuthorization];
[self.manager startUpdatingLocation];
}else{
NSLog(@"IOS7");
//开始获取用户位置
[self.manager startUpdatingLocation];
}

接下来便需要实现一些代理方法来方便我们获取用户位置,以下为常用的部分代理协议

1.用户授权状态改变后调用

 //在此判断授权状态并进行相对应的操作
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if (status == kCLAuthorizationStatusAuthorizedAlways ||
status == kCLAuthorizationStatusAuthorizedWhenInUse) {
NSLog(@"授权成功");
//授权成功后开始监听 获取位置
[self.manager startUpdatingLocation];
}
}

2.在更新用户位置时调用的方法

 //可以在这里进行反地理编码获取位置信息
- (void)locationManager:(CLLocationManager *)managerdidUpdateLocations:(NSArray *)locations{
//反地理编码使用位置信息反编码
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *newLocation = [locations lastObject];
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark *place in placemarks) {
//通过CLPlacemark可以输出用户位置信息
NSLog(@"%@ %@ %lf %lf",place.name, place.addressDictionary, place.location.coordinate.latitude,place.location.coordinate.longitude);
}
}];
}

3.用户手机头部方向改变时调用的方法

 //在更新用户头部方向时调用,适用方向指定操作
- (void)locationManager:(CLLocationManager *)managerdidUpdateHeading:(CLHeading *)newHeading

至此Corelocation的配置就已完成,接下来是关于地图配置的方法

地图控件的基本使用

首先要导入地图框架 #import <MapKit/MapKit.h>

设置地图控件属性,并懒加载实现及其属性配置

 //地图控件,其中region属性个人觉得在代理中设置比较合适
@property (nonatomic, strong) MKMapView *mapView;
- (MKMapView *)mapView{
if (_mapView == nil) {
_mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
_mapView.delegate = self;
/*
MKUserTrackingModeNone = 0 默认不跟踪
MKUserTrackingModeFollow, 追踪位置
MKUserTrackingModeFollowWithHeading 追踪位置及其头部指向
*/
//用户追踪模型,如不设置将无法追踪用户
_mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
/*
MKMapTypeStandard = 0,普通模式(默认模式)
MKMapTypeSatellite, 卫星模式
MKMapTypeHybrid 混合模式
*/
_mapView.mapType = MKMapTypeStandard;
}
return _mapView;
}

最后将_mapView添加进self.view中,整个地图相当于就可以加载完成了。下面介绍MKMapView的一部分代理方法及其调用时机

 #pragma mark - MKMapViewDelegate 代理方法
/**
* 更新用户位置是调用
*
* @param mapView 题图
* @param userLocation 用户位置
*/
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
NSLog(@"更新用户位置");
//设置用户位置中心点
[self.mapView setCenterCoordinate:userLocation.coordinate animated:YES];
//范围设置MKCoordinateSpanMake中的代表经纬度范围
MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.coordinate, MKCoordinateSpanMake(0.1, 0.1));
//设置中心点范围
[self.mapView setRegion:region animated:YES];
}
/**
* 地图将要开始渲染时调用
*/
- (void)mapViewWillStartRenderingMap:(MKMapView *)mapView{
NSLog(@"%s",__func__);
}
/**
* 地图完成渲染时调用
*/
- (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered{
NSLog(@"%s",__func__);
}
/**
* region(范围)即将改变时调用
*/
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{
NSLog(@"%s",__func__);
}
/**
* region(范围)完成改变时调用
*/
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
NSLog(@"%s",__func__);
}
/**
* 追踪模型改变时调用
*/
- (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated{
NSLog(@"%s",__func__);
}
/**
* 设置大头针时调用 类似于tableViewCell设置
*/
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
51 //在其中设置MKAnnotationVIew,有重用机制
52}

另外还有其他一些代理方法就不一一介绍了,剩下的就看需要 在对应的方法里添加想进行的操作。