iOS关于高德SDK详解和简单使用

 根据现在项目需求,地图,定位成了现在app的很常用的一个技术点,现在国内大众化的地图框架个人总结一下,不是很全面,但是大致满足大家的需求。

  1,系统自带mapkit框架,这个框架维护性比较高,但是封装起来比较繁杂。

  2,第三发SDK,我们一般选择高德或者百度。

下面我来总结一下我用高德SDK的心得,下面来看看使用流程

  1,我们先申请一个appkey,申请appkey必须注册高德开发者

    

  2,高德SDK的下载,现在SDK分别有三个库,根据你的app 里面的集成需求,可以选择性的下载添加到自己的工程里,他们分别是 2D地图库,3D地图库,还有搜索库;

  3,添加SDK进自己的项目(工程)里,添加的时候注意路径问题,添加完高德SDK之后,我们还需要添加一些系统自带库,有了这些才能支持高德SDK的运行,他们分别如下图

  

  4,运行环境的配置,在TARGETS->Build Settings->Other Linker Flaggs 中添加-ObjC。

或者大家有熟悉CocoaPods的同学想省去配置环境和路径的问题,那么我们就可以采用pods来配置他,分别搜索AMap3DMap,AMap2DMap,AmapSearch,大家一看大致就知道这三个的用途了,如果不知道怎么用pods来管理的话,我会在下一篇博客里面讲解CocoaPods的使用。

  5,接下来大家最关心的地方来了,那就是怎么实现显示地图的代码,下面我们就来看看里面的核心类,

  首先我们要现实地图,MAMapView这是实现地图的关键类,下面看怎么来展示地图在我们的项目里面,我们这里先说说2D的实现吧,

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    [MAMapServices sharedServices].apiKey = @"a2e716827857a145e86e99ea08cfe15f";
    
    _mapView = [[MAMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _mapView.mapType = MAMapTypeSatellite;//设置地图样式,卫星
    _mapView.showTraffic = YES; //打开实时交通路况
    _mapView.delegate = self;
    _mapView.logoCenter = CGPointMake(CGRectGetWidth(self.view.bounds)-55, 450);//设置地图logo的中心点
    _mapView.showsCompass= YES; // 设置成 NO 表示关闭指南针;YES 表示显示指南针
    
    _mapView.showsScale = YES; //设置成 NO 表示不显示比例尺;YES 表示显示比例尺
    _mapView.scaleOrigin= CGPointMake(_mapView.scaleOrigin.x, 22); //设置比例尺位置
    _mapView.compassOrigin= CGPointMake(_mapView.compassOrigin.x, 22); //设置指南针位置

    _mapView.showsUserLocation = YES; //开启地图定位
    
    
    [_mapView setUserTrackingMode:MAUserTrackingModeFollow animated:YES]; //设置用户根宗模式
    
    
    [self.view addSubview:_mapView];
    
    MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];//初始化标注
    pointAnnotation.coordinate = CLLocationCoordinate2DMake(39.989631, 116.481018); //设置标注地理坐标
    pointAnnotation.title = @"方恒国际";//设置标注标题
    pointAnnotation.subtitle = @"阜通东大街 6 号";//设置标注子标题
    [_mapView addAnnotation:pointAnnotation];//添加标注
    
}
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
        if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
            static NSString *pointReuseIndetifier = @"pointReuseIndetifier";
            MAPinAnnotationView*annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
            if (annotationView == nil) {
                annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndetifier];
            }
            annotationView.canShowCallout= YES; //设置气泡可以弹出,默认为 NO
        
            annotationView.animatesDrop = YES; //设置标注动画显示,默认为 NO

            annotationView.draggable = YES; //设置标注可以拖动,默认为 NO annotationView.pinColor = MAPinAnnotationColorPurple;
            return annotationView;
        }
        return nil;
    
        
}


- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation{
//    NSLog("latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.l ongitude);
    NSLog(@"latitude : %f,longitude: %f", userLocation.coordinate.latitude,userLocation.coordinate.longitude);
    
}

我们注意看看ViewWillApper里面,这些代码,这里都是设置地图的一些属性的,显示地图看起来是不是很简单,定位也是一个简单技术点,但是在这里我们再来看看重点,我们一半的应用都是输入一个地址,得到一个标注,或者根据定位得到地址,

  根据地址插入地图上面一个标注,标注初始化的时候我们传入了一个经纬度,

  定位得到地址,定位可以得到经纬度,标注在地图上面,但是目的是输出地址,

 那么这两个都涉及到了一个共同的问题,一个是将经纬度转出地址,一个是根据地址转出经纬度,那么这两个是一个逆向过程,我们下面我们看看怎么实现这个过程的,在地图开发中,这个就是我们所谓的地理编码和反编码。

现在我们今天讲的是高德SDK,那么用的编码过程也是

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _search = [[AMapSearchAPI alloc] initWithSearchKey:@"a2e716827857a145e86e99ea08cfe15f" Delegate:self];  //初始化搜索
    AMapGeocodeSearchRequest *georequest = [[AMapGeocodeSearchRequest alloc] init]; //构造一个request对象
    georequest.searchType = AMapSearchType_Geocode; //设置为地理编码样式
    georequest.address = @"北大"; //地址
    georequest.city = @[@"北京"];//所在城市
    [_search AMapGeocodeSearch:georequest]; //发起地理编码
    
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark 地理编码成功的回调
- (void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response
{

    if (response.count == 0) {
        return;
    }
    NSString *string = [NSString stringWithFormat:@"%ld", response.count];
    for (AMapTip *tip in response.geocodes) {//response.geocodes所有跟地址匹配的地点
        NSLog(@"%@", [NSString stringWithFormat:@"%@", tip.description]);
    }
    
}

反向地理编码跟正向基本一致下面是代码

    _search = [[AMapSearchAPI alloc] initWithSearchKey:@"a2e716827857a145e86e99ea08cfe15f" Delegate:self];  //初始化搜索
    AMapReGeocodeSearchRequest *georequest = [[AMapReGeocodeSearchRequest alloc] init]; //构造一个request对象
    georequest.searchType = AMapSearchType_ReGeocode; //设置为反地理编码样式
    georequest.location = [AMapGeoPoint locationWithLatitude:39.000 longitude:116.00];//设置所在地里位置的经纬度
    georequest.radius = 1000;//搜索半径
    georequest.requireExtension = YES;// 是否返回扩展信息,默认为 NO
    [_search AMapGeocodeSearch:georequest]; //发起反地理编码
#pragma mark 反地理编码成功
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
    if (response.regeocode != nil) {
        //处理搜索结果
        NSString *result = [NSString stringWithFormat:@"%@", response.regeocode];
    }
}

在对今天的技术点做一个总结,

  1,实现地图的展示,

  2,插入标注,插入的时候要给经纬度

  3,定位服务,定位服务获取的是经纬度,

  4,地理编码和反编码

  5,定位,标注这两个点要注意,一般我们都是给定一个地址想得到标注在地图上面,那么我们必须对其进行地理编码,定位获取的经纬度,我们需要看到的是地址,那么我们要将经纬度反地理编码成地址

今天高德SDK的简单实用就到这里了,如果想实现自定义图层,搜索路径,这些请参看高德SDK官方文档。

  

原文地址:https://www.cnblogs.com/zouhaoit/p/4441178.html