高德地图正/逆向地理编码

2021-05-10  本文已影响0人  xyZHua

应该场景描述:后台返回给我的地址,我需要将地址转成 经纬度,进行导航。
记录原因:网上相关文档,因为版本原因,更新不全面,无法快速实现功能

使用步骤:

  1. 项目中导入 高德地图的 SDK ,我用的是pod, 在pod文件中加入下面代码,并执行 pod install .
#高德地图
    pod 'AMapSearch'
    pod 'AMapLocation'
    pod 'AMapNavi'
  1. 在需要使用的 XXX controller.h 导入头文件
#import <AMapNaviKit/MAMapKit.h>    //  创建地图需要
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
  1. XXX controller.m 中遵循 代理
@interface XXXMapController ()
<MAMapViewDelegate,
AMapSearchDelegate>
@end
  1. 功能代码
- (void)viewDidLoad {
    [super viewDidLoad];
    // 隐藏navBar
    [self navBarNeedHidden:YES andAnimation:NO];
    
    [self configSubViews];
}

-(void)configSubViews {
    [AMapServices sharedServices].enableHTTPS = YES;
    // 配置用户APP Key --> 自己在高德平台去申请
    [[AMapServices sharedServices] setApiKey:@"xxxxxxxxxxxxxxxxxxx"];
    
    ///初始化地图
    self.maMapView = [[MAMapView alloc] initWithFrame:self.mapBGView.bounds];
    self.maMapView.zoomLevel = 14.5f;
    self.search  = [[AMapSearchAPI alloc] init];
//    self.maMapView.delegate = self;
    self.search.delegate = self;
//发起正向地理编码
    AMapGeocodeSearchRequest *searchRequest = [[AMapGeocodeSearchRequest alloc] init];
    // 将字符串的地址 通过正向地理编码转换为 经纬度坐标点
    searchRequest.address = self.addressStr;
    [self.search AMapGeocodeSearch: searchRequest];
// 发起逆向地理编码
// AMapReGeocodeSearchRequest *searchRequest = [[AMapReGeocodeSearchRequest alloc] init];
    // searchRequest.location.latitude = xx.xxxxxx;
   // searchRequest.location.longitude = xx.xxxxxx;
   //  [self.search AMapReGoecodeSearch:searchRequest];
       ///把地图添加至view
       [self.mapBGView addSubview:self.maMapView];
   
}


#pragma mark -- MAMapViewDelegate
// 自定义标记点图标,暂时不需要自定义
/*
-(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
       {
           static NSString *reuseIndetifier = @"annotationReuseIndetifier";
           MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
           if (annotationView == nil)
           {
               annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation
   reuseIdentifier:reuseIndetifier];
           }
           annotationView.image = [UIImage imageNamed:@"warehouseMap"];
           //设置中心点偏移,使得标注底部中间点成为经纬度对应点
           annotationView.centerOffset = CGPointMake(0, -18);
           return annotationView;
       }
       return nil;
   
}
*/

#pragma mark -- AMapSearchDelegate
// 正向地理编码(将文字地址转换为坐标点的经纬度)
-(void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response {
    if (response.geocodes.count == 0) {

            return;
        }
    
    AMapGeoPoint *point = response.geocodes[0].location;
    self.latitude = point.latitude;
    self.longitude = point.longitude;
    self.maMapView.centerCoordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude);
    MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
    pointAnnotation.coordinate = CLLocationCoordinate2DMake(self.latitude, self.longitude);
    [self.maMapView addAnnotation:pointAnnotation];
    
}
// 逆向地理编码(将文字地址转换为坐标点的经纬度)
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response

{

    if (![DDUtilsisNullOrNil:response.regeocode]) {

        AMapReGeocode *regeocode = response.regeocode;

        CLLocationDegrees latitude = request.location.latitude;

        CLLocationDegrees longitude = request.location.longitude;

        NSString *address = regeocode.formattedAddress; // 获得检索位置 

    } 

}
上一篇下一篇

猜你喜欢

热点阅读