ios 高德地图

1,400 阅读1分钟

先附上高德地图官网

第一步:cocopods安装sdk

  pod 'AMap3DMap' #3D地图sdk
  pod 'AMapSearch' #地图SDK搜索功能 
  pod 'AMapLocation' #定位SDK 

高德 iOS SDK 的 Pod 库的名称如下图:


第二步:plist文件配置

在plist文件中添加字段Privacy - Location When In Use Usage Description和Privacy - Location Always and When In Use Usage Description,然后集成就完毕了^-^。

第三步:AppDelegate配置

在AppDelegate文件导入头文件#import<AMapFoundationKit/AMapFoundationKit.h>,并添加如下代码

[AMapServices sharedServices].apiKey = @"您的Key";

第四步:在调用的视图中导入如下头文件:

#import <AMapFoundationKit/AMapFoundationKit.h>
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchAPI.h>

地图(MKMapView)的使用

- (MAMapView *)header {
    if (_header == nil) {
        _header = [[MAMapView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth,kScreenHeight - [UIApplication sharedApplication].statusBarFrame.size.height - 44)];
        _header.delegate = self;
        // 打开定位
        _header.showsUserLocation = YES;
        _header.userTrackingMode = MAUserTrackingModeFollow;
        // 设定定位精度。默认为kCLLocationAccuracyBest
        _header.desiredAccuracy = kCLLocationAccuracyBest;
        // 是否显示指南针
        _header.showsCompass = NO;
        // 设定定位的最小更新距离。默认为kCLDistanceFilterNone,会提示任何移动
        _header.distanceFilter = 15.0f;
        // 是否显示比例尺,默认为YES
        _header.showsScale = YES;
        // 是否支持缩放,默认为YES
        _header.zoomEnabled = YES;
        // 是否支持平移,默认为YES
        _header.scrollEnabled = YES;
        // 缩放级别
        _header.zoomLevel = 15;
    }
    return _header;
} 

获取地理位置

- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response {
    AMapReGeocode *reGeocodeModel = response.regeocode;
    
    if (response.regeocode != nil) {
        
        MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
        _pointAnnotaiton = pointAnnotation;
        pointAnnotation.coordinate = self.header.userLocation.coordinate;
        
        //去掉重复大头针
        [self.header removeOverlays:self.header.overlays];
        [self.header removeAnnotations:self.header.annotations];
        //将大头针添加到地图中
        [self.header addAnnotation:pointAnnotation];
        //默认选中气泡
        [self.header selectAnnotation:pointAnnotation animated:YES];
        
    }
    
    // 坐标
    NSLog(@"%@",response.regeocode.formattedAddress);
} 

移动地图大头针位置跟新在地图中间

- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    self.pointAnnotaiton.coordinate = mapView.centerCoordinate;
}

周边搜索回调

-(void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (response.pois.count>0) {
            self.dataArray = [response.pois mutableCopy];
            [self.tableView reloadData];
        }
    });
}

搜索请求发起后的失败回调

-(void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error{
    NSLog(@"request: %@------error:  %@",request,error);
}

定位更新回调

-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
    if(updatingLocation)
    {
        //取出当前位置的坐标
        NSLog(@"latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
        _currentLocation = [userLocation.location copy];
    }
    
}

效果如下图: