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

最终效果图:

iOS 高德地图定位及地理反编码的简明教程_第1张图片

 

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

你可能感兴趣的:(iOS 高德地图定位及地理反编码的简明教程)