iOS集成高德地圖SDK

前言:關(guān)于這次集成高德地圖,打算分幾個(gè)內(nèi)容定位 POI檢索 導(dǎo)航 線路規(guī)劃,現(xiàn)在只是簡單地實(shí)現(xiàn)了前兩個(gè)功能,先記錄一下吧。
至于集成SDK的過程很簡單,需要注意的只是Info.plist中的添加兩個(gè)字段
Privacy - Location When In Use Usage Description 定位權(quán)限(使用時(shí)定位)
Allow Arbitrary Loads

  • 定位

(1)添加大頭針
先導(dǎo)入頭文件
#import <MAMapKit/MAMapKit.h> #import <AMapFoundationKit/AMapFoundationKit.h> #import <AMapSearchKit/AMapSearchKit.h>
懶加載創(chuàng)建mapView

    if (_mapView == nil) {
        _mapView = [[MAMapView alloc]initWithFrame:self.view.bounds];
        //設(shè)置進(jìn)入地圖的縮放比例
        [_mapView setZoomLevel:17.5 animated:YES];
        //地圖跟著位置移動(dòng)
        [_mapView setUserTrackingMode: MAUserTrackingModeFollow animated:NO];
        ///如果您需要進(jìn)入地圖就顯示定位小藍(lán)點(diǎn),則需要下面兩行代碼
        _mapView.showsUserLocation = YES;
        //如果不寫這句話,就不會(huì)顯示藍(lán)色圓圈,也不會(huì)自動(dòng)定位,這個(gè)貌似無法更改大小
        _mapView.userTrackingMode = MAUserTrackingModeFollow;
        _mapView.delegate = self;
    }
    return _mapView;
}

創(chuàng)建大頭針重要的一點(diǎn)是要實(shí)現(xiàn)-(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation代理方法
此時(shí)我們并沒有給大頭針加上經(jīng)緯度和標(biāo)題,看很多demo都是直接賦值,并沒有根據(jù)當(dāng)前用戶的位置設(shè)置大頭針,經(jīng)過我不斷地踩坑,解決了這個(gè)問題。
要獲取當(dāng)前用戶的位置及位置的title和subTitle,要實(shí)現(xiàn)<AMapSearchKit/AMapSearchKit.h>中的兩個(gè)代理方法:
// 用戶地址發(fā)生更新后執(zhí)行- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation(這個(gè)方法用于獲取當(dāng)前用戶的經(jīng)緯度)
// 反編碼回調(diào)函數(shù)- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response(這個(gè)方法用于獲取當(dāng)前位置的title和subTitle)
此時(shí)我們就可以使用大頭針的類MAPointAnnotation,創(chuàng)建一個(gè)全局對(duì)象,在代理方法中對(duì)其設(shè)置

- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {
    //將用戶地址傳入?yún)?shù)currentLocation
    currentLocation = [userLocation.location copy];
    //獲得當(dāng)前地理編碼后,進(jìn)行反編碼,獲得當(dāng)前定位點(diǎn)的信息
    [self reGeoAction];
    
}
//地理反編碼函數(shù)
-(void)reGeoAction {
    if (currentLocation) {
        //生成一個(gè)地理反編碼的搜索
        AMapReGeocodeSearchRequest *request = [[AMapReGeocodeSearchRequest alloc] init];
        
        request.location = [AMapGeoPoint locationWithLatitude:currentLocation.coordinate.latitude longitude:currentLocation.coordinate.longitude];
        [self.searchAPI AMapReGoecodeSearch:request];
    }
}
// 反編碼回調(diào)函數(shù)
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response {
    //將搜索結(jié)果傳給用戶定位點(diǎn),這樣點(diǎn)擊氣泡就可以顯示出詳細(xì)信息
    NSString *title = response.regeocode.addressComponent.city;
    if (title.length ==0) {
        title = response.regeocode.addressComponent.province;
    }
    self.mapView.userLocation.title = title;
    self.mapView.userLocation.subtitle = response.regeocode.formattedAddress;
    [self.mapView selectedAnnotations];
    self.mapView.centerCoordinate = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
    pointAnnotation.coordinate = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
    pointAnnotation.title = title;
    pointAnnotation.subtitle = response.regeocode.formattedAddress;

}


//添加大頭針
-(MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{
    
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        MAPinAnnotationView *annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil)
        {
            annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }
        annotationView.canShowCallout= YES; //設(shè)置氣泡可以彈出,默認(rèn)為NO
        annotationView.animatesDrop = YES;  //設(shè)置標(biāo)注動(dòng)畫顯示,默認(rèn)為NO
        annotationView.draggable = YES;     //設(shè)置標(biāo)注可以拖動(dòng),默認(rèn)為NO
        annotationView.pinColor = MAPinAnnotationColorPurple;
        return annotationView;
    }
    return nil;
}

效果圖(最好用真機(jī)測試,模擬器不準(zhǔn)確):

定位大頭針

(2)添加覆蓋圓

- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response {
 
    MACircle *circle = [MACircle circleWithCenterCoordinate:currentLocation.coordinate radius:1000];
    [self.mapView addOverlay:circle];
}
//添加覆蓋的圓圈的代理方法
-(MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay{
    
    if ([overlay isKindOfClass:[MACircle class]])
    {
        MACircleRenderer *circleView = [[MACircleRenderer alloc] initWithCircle:overlay];
        circleView.lineWidth = 5.f;
        circleView.strokeColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.8];
        circleView.fillColor = [UIColor colorWithRed:1.0 green:0.8 blue:0.0 alpha:0.8];
        return circleView;
    }
    return nil;
}
  • POI周邊檢索

    自定義一個(gè)搜索框,將搜索框中的字符串作為POI檢索的關(guān)鍵字,然后將搜索到的信息展示出來,實(shí)現(xiàn)過程很簡單,先上一張效果圖:
    POI檢索效果圖:
    POI檢索效果圖:

    )
    效果圖直接去github上看吧,簡書不知道為什么上傳不了動(dòng)圖了.
    Demo地址
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容