iOS 高德地圖定位及地理反編碼的簡明教程

最終效果圖:git

 

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

二,開啓定位spa

     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, 實現代理協議並開啓定位code

@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, 得到用戶當前經緯度orm

- (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];
}

三, 根據經緯度經過地理反編碼獲得當前街道信息ip

- (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值查找緣由所在
    }
}

四, 顯示地圖並對當前用戶位置進行定位跟隨get

- (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
相關文章
相關標籤/搜索