iOS 高德地图定位及地理反编码的简明教程

最终效果图:

 

一, 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<CLLocationManagerDelegate,MKMapViewDelegate>

@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<CLLocation *> *)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<CLPlacemark *> * _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<UITouch *> *)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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值