zoukankan      html  css  js  c++  java
  • Corelocation及地图控件学习笔记

    Corelocation基本使用

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    地图控件的基本使用

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

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

     1 //地图控件,其中region属性个人觉得在代理中设置比较合适
     2 @property (nonatomic, strong) MKMapView *mapView;
     3 - (MKMapView *)mapView{
     4     if (_mapView == nil) {
     5         _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
     6         _mapView.delegate = self;
     7         /*
     8          MKUserTrackingModeNone = 0 默认不跟踪
     9          MKUserTrackingModeFollow,  追踪位置
    10          MKUserTrackingModeFollowWithHeading 追踪位置及其头部指向
    11          */
    12         //用户追踪模型,如不设置将无法追踪用户
    13         _mapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;
    14         /*
    15          MKMapTypeStandard = 0,普通模式(默认模式)
    16          MKMapTypeSatellite,   卫星模式
    17          MKMapTypeHybrid       混合模式
    18          */
    19         _mapView.mapType = MKMapTypeStandard;
    20     }
    21     return _mapView;
    22 }

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

     1 #pragma mark - MKMapViewDelegate 代理方法
     2 /**
     3  *  更新用户位置是调用
     4  *
     5  *  @param mapView      题图
     6  *  @param userLocation 用户位置
     7  */
     8 - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
     9     NSLog(@"更新用户位置");
    10     //设置用户位置中心点
    11     [self.mapView setCenterCoordinate:userLocation.coordinate animated:YES];
    12     //范围设置MKCoordinateSpanMake中的代表经纬度范围
    13     MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.coordinate, MKCoordinateSpanMake(0.1, 0.1));
    14     //设置中心点范围
    15     [self.mapView setRegion:region animated:YES];
    16 }
    17 /**
    18  *  地图将要开始渲染时调用
    19  */
    20 - (void)mapViewWillStartRenderingMap:(MKMapView *)mapView{
    21     NSLog(@"%s",__func__);
    22 }
    23 /**
    24  *  地图完成渲染时调用
    25  */
    26 - (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered{
    27     NSLog(@"%s",__func__);
    28 }
    29 /**
    30  *  region(范围)即将改变时调用
    31  */
    32 - (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{
    33     NSLog(@"%s",__func__);
    34 }
    35 /**
    36  *  region(范围)完成改变时调用
    37  */
    38 - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
    39     NSLog(@"%s",__func__);
    40 }
    41 /**
    42  *  追踪模型改变时调用
    43  */
    44 - (void)mapView:(MKMapView *)mapView didChangeUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated{
    45     NSLog(@"%s",__func__);
    46 }
    47 /**
    48  *  设置大头针时调用 类似于tableViewCell设置
    49  */
    50 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
    51 //在其中设置MKAnnotationVIew,有重用机制
    52}

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

    -----------------------如有转发请注明出处 ,谢谢!
  • 相关阅读:
    select和epoll原理和区别
    linux网络编程中的基本概念
    linux 基本概念
    进程与线程(1)- 基本概念
    CI持续集成
    git基本操作(入门)
    pytest特色与实用插件
    使用pabot并行执行robotframework用例
    如何编写测试用例
    前端_vue-cli+element-ui+AceEditor+codemirror+electron-vue
  • 原文地址:https://www.cnblogs.com/purple-sweet-pottoes/p/4645162.html
Copyright © 2011-2022 走看看