博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 高德地图定位及地理反编码的简明教程
阅读量:6281 次
发布时间:2019-06-22

本文共 4218 字,大约阅读时间需要 14 分钟。

  hot3.png

最终效果图:

 

一, plist及frame的配置

      1 ,info.plist文件中添加 Privacy - Location When In Use Usage Description(需要时开启定位,另一个是Privacy - Location Always Usage Description 一直开启定位)。

      2, 添加framework框架,MapKit.framework与CoreLocation.framework,并分别在需要定位的视图中导入头文件:CoreLocation/CoreLocation.h 与 MapKit/MapKit.h

二,开启定位

     1, 在项目中加入代理协议:CLLocationManagerDelegate,MKMapViewDelegate

@interface ViewController : UIViewController
@property (nonatomic,strong) CLLocationManager *locationManager;@property (nonatomic,strong) CLGeocoder *geocoder;@property (nonatomic,strong) MKMapView *mapViewL;@end

    2, 实现代理协议并开启定位

@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        self.locationManager = [[CLLocationManager alloc]init];    self.locationManager.delegate = self;    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;    self.geocoder = [[CLGeocoder alloc]init];    self.placeDic = [[NSDictionary alloc]init];    MKUserLocation *userLOCation = [[MKUserLocation alloc]init];    _userLOcation = userLOCation;        [self startLocationForYou];        _placeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 300, 50)];    [self.view addSubview:_placeLabel];        _mapViewL = [[MKMapView alloc]initWithFrame:CGRectMake(0, 200, self.view.bounds.size.width, self.view.bounds.size.height - 200)];    _mapViewL.delegate = self;    [self.view addSubview:_mapViewL];    _mapViewL.userTrackingMode = MKUserTrackingModeFollow;    _mapViewL.mapType = MKMapTypeStandard;}//开始定位- (void)startLocationForYou{        if (![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse){                NSLog(@"定位功能已经打开");        [_locationManager requestWhenInUseAuthorization];    }        //调用定位信息    [self.locationManager startUpdatingLocation];}

    3, 获得用户当前经纬度

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray
*)locations{ CLLocation *location = [locations lastObject]; CLLocationCoordinate2D coord = location.coordinate;// NSLog(@"经度:%f 纬度:%f 海拔: %f 航向:%f 速度:%f",coord.longitude,coord.latitude,location.altitude,location.course,location.speed); [self getGeocoder:coord.longitude Atitude:coord.latitude]; // [manager stopUpdatingLocation];}

三, 根据经纬度通过地理反编码得到当前街道信息

- (void)getGeocoder:(CLLocationDegrees )longitude Atitude:(CLLocationDegrees )atitude{        CLLocation *location = [[CLLocation alloc]initWithLatitude:atitude longitude:longitude];    [_geocoder reverseGeocodeLocation:location completionHandler:^(NSArray
* _Nullable placemarks, NSError * _Nullable error) { CLPlacemark *placeMark = [placemarks firstObject]; // if (self.placeDic.count == 0){ self.placeDic = placeMark.addressDictionary; [self labelView:_placeDic[@"FormattedAddressLines"][0]];// NSLog(@"详细地址:%@ ==== ",placeMark.addressDictionary);// } }];}- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ if (error.code == kCLErrorDenied) { NSLog(@"Error:%@",error); // 提示用户出错原因,可按住Option键点击 KCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在 }}

四, 显示地图并对当前用户位置进行定位跟随

- (void)touchesBegan:(NSSet
*)touches withEvent:(UIEvent *)event{ [self startLocationForYou]; _mapViewL.userTrackingMode = MKUserTrackingModeFollow; MKCoordinateSpan span = MKCoordinateSpanMake(0.002, 0.002); MKCoordinateRegion regin = MKCoordinateRegionMake(_userLOcation.location.coordinate, span); [_mapViewL setRegion:regin animated:YES];}- (void)labelView:(NSString *)placeLabel{ self.placeLabel.text = placeLabel; self.placeLabel.numberOfLines = 0; self.placeLabel.font = [UIFont systemFontOfSize:15]; }- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{ _userLOcation = userLocation; //Setting area MKCoordinateSpan span = MKCoordinateSpanMake(0.002, 0.002); MKCoordinateRegion regin = MKCoordinateRegionMake(userLocation.location.coordinate, span); [_mapViewL setRegion:regin animated:YES]; }@end

 

转载于:https://my.oschina.net/Kuture/blog/761775

你可能感兴趣的文章
JavaScript—数组(17)
查看>>
Android 密钥保护和 C/S 网络传输安全理论指南
查看>>
以太坊ERC20代币合约优化版
查看>>
Why I Began
查看>>
同一台电脑上Windows 7和Ubuntu 14.04的CPU温度和GPU温度对比
查看>>
js数组的操作
查看>>
springmvc Could not write content: No serializer
查看>>
Python系语言发展综述
查看>>
新手 开博
查看>>
借助开源工具高效完成Java应用的运行分析
查看>>
163 yum
查看>>
第三章:Shiro的配置——深入浅出学Shiro细粒度权限开发框架
查看>>
80后创业的经验谈(转,朴实但实用!推荐)
查看>>
让Windows图片查看器和windows资源管理器显示WebP格式
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
vim使用点滴
查看>>
embedded linux学习中几个需要明确的概念
查看>>
mysql常用语法
查看>>
Morris ajax
查看>>