高德云圖 iOS SDK 使用總結(jié)

APP開發(fā)需求:加載地圖,定位到用戶當(dāng)前的位置,并標(biāo)注附近的坐標(biāo)點(上圖中的藍(lán)色標(biāo)注點)。坐標(biāo)數(shù)據(jù)由業(yè)務(wù)人員提供。同時還需要將用戶地理位置(城市)上傳到服務(wù)端。地圖sdk選擇高德家的,高德地圖對開發(fā)者比較友好。首先,注冊高德開發(fā)者賬號,注冊之后登錄帳號,申請用戶Key

開發(fā)環(huán)境配置

高德提供CocoaPods安裝方式,這點比百度地圖做的好。自動安裝比手動安裝方便太多了。在Podfile中添加下面兩行

pod 'AMap2DMap'
pod 'AMapSearch' #搜索服務(wù)SDK

運行命令pod install 安裝高德sdk。更詳細(xì)的步驟移步高德開發(fā)者配置文檔。

地圖功能

首先,引入頭文件

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

初始化mapview

self.mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
self.mapView.delegate = self;
self.mapView.showsUserLocation = YES;   //YES 為打開定位,NO為關(guān)閉定位
self.mapView.showsCompass = NO;
self.mapView.showsScale = NO;
self.mapView.userTrackingMode = MAUserTrackingModeFollow; // 追蹤用戶地理位置更新
self.mapView.zoomLevel = 16.1;
self.mapView.alpha = 0.8;

實現(xiàn) MAMapViewDelegate,獲取用戶當(dāng)前經(jīng)緯坐標(biāo):

#pragma mark - MAMapViewDelegate
-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
    if(updatingLocation){
        //取出當(dāng)前位置的坐標(biāo)
        self.currentCoordinate = userLocation.coordinate;
    }
}

- (void)mapView:(MAMapView *)mapView didFailToLocateUserWithError:(NSError *)error {
    DLog(@"%@",error);
}

當(dāng)用戶手動滑動地圖后,可點擊界面上的定位按鈕,將地圖中心定位到用戶當(dāng)前位置

-(void)locationButtonClicked {
    if (self.currentCoordinate.latitude > 0) { 
        CLLocationCoordinate2D myCoordinate = self.currentCoordinate;
        MACoordinateRegion theRegion = MACoordinateRegionMake(myCoordinate, MACoordinateSpanMake(0.2, 0.2)); 
        [self.mapView setScrollEnabled:YES];
        [self.mapView setRegion:theRegion animated:YES];
        [self.mapView setZoomLevel:16.1 animated:NO];
    }
}

通過地理位置經(jīng)緯坐標(biāo)獲取城市名稱,即逆地理編碼

逆地理編碼,又稱地址解析服務(wù),是指從已知的經(jīng)緯度坐標(biāo)到對應(yīng)的地址描述(如行政區(qū)劃、街區(qū)、樓層、房間等)的轉(zhuǎn)換。常用于根據(jù)定位的坐標(biāo)來獲取該地點的位置詳細(xì)信息,與定位功能是黃金搭檔。

進(jìn)行逆地編碼時,請求參數(shù)類為 AMapReGeocodeSearchRequest,location為必設(shè)參數(shù)。

- (void)searchLocationWithCoordinate2D:(CLLocationCoordinate2D )coordinate {
    //構(gòu)造AMapReGeocodeSearchRequest對象
    AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
    regeo.location = [AMapGeoPoint locationWithLatitude:coordinate.latitude longitude:coordinate.longitude];
    regeo.radius = 10000;
    regeo.requireExtension = YES;
    
    //發(fā)起逆地理編碼
    [self.searchAPI AMapReGoecodeSearch: regeo];
}

實現(xiàn)逆地理編碼的回調(diào)函數(shù)

#pragma mark - AMapSearchDelegate
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
    if(response.regeocode != nil)
    {
        //通過AMapReGeocodeSearchResponse對象處理搜索結(jié)果
        NSString *city = response.regeocode.addressComponent.city;
        if (!city || [city length] == 0) {
            city = response.regeocode.addressComponent.province; // 直轄市時獲取此字段
        }

        self.city = city;
    }
}

云檢索

開發(fā)檢索自有數(shù)據(jù)的步驟:

第一步,數(shù)據(jù)存儲。開發(fā)者需要將待檢索數(shù)據(jù)存入云圖,并對要檢索的字段建立索引管理。

第二步,檢索。利用SDK為開發(fā)者提供的接口檢索自己的數(shù)據(jù)。

第三步,展示。開發(fā)者可根據(jù)自己的實際需求以多種形式(如結(jié)果列表、地圖模式等)展現(xiàn)自己的數(shù)據(jù)。

1.數(shù)據(jù)存儲

由于要導(dǎo)入的數(shù)據(jù)量比較多,可以調(diào)用云圖API批量上傳數(shù)據(jù)。利用API創(chuàng)建云圖表,需先申請Web服務(wù)APIkey,此key和ios端的key是兩個獨立的key,這個key用于webapi調(diào)用。創(chuàng)建表:

 > $apikey=xxxxxxxx;
 > curl -d "key=$apikey&name=test" http://yuntuapi.amap.com/datamanage/table/create

status=1表明創(chuàng)建成功,同時返回的還有tableid,根據(jù)這個tableid就可以做數(shù)據(jù)導(dǎo)入

{"info":"OK","status":1,"tableid":"565c0fa8e4b0dce2a8eabc81"}%

批量上傳數(shù)據(jù)只能以文件的形式:

向指定tableid的數(shù)據(jù)表中通過上傳文件的方式創(chuàng)建多條數(shù)據(jù)。該接口為異步接口,請求后立即返回后臺進(jìn)程batchid,通過批量創(chuàng)建數(shù)據(jù)進(jìn)度查詢接口查看任務(wù)執(zhí)行進(jìn)度和結(jié)果。

文件格式和大小有限制:

  • csv文件的二進(jìn)制流,使用UTF8、GBK編碼
  • 數(shù)據(jù)量不超過10000條且文件大小不超過10M
  • 字段總數(shù)不超過40個。
  • 字段命名規(guī)則:以英文字母開頭,僅由英文字母、數(shù)字、下劃線組成,總長度不超過20位

文件內(nèi)容如下,name,address,longitude,latitude這四個字段是必須的,可以自定義其他字段。

name,address,longitude,latitude
543691,北京市東城區(qū)故宮博物院,116.312361,39.843083
543692,北京海淀區(qū)中關(guān)村大街1號海龍大廈,116.365686,40.034993

上傳腳本:

#!/bin/bash

key=xxxxxxx
tableid=565c0fa8e4b0dce2a8eabc81
uploadDir=/User/upload

for i in `cat uploadfile.txt`  #uploadfile.txt包含了所有要上傳的文件名稱
do
echo -n "$i "
curl -F "file=@$uploadDir/$i" http://yuntuapi.amap.com/datamanage/data/batchcreate\\\\?key\\\\=$key\\\\&tableid\\\\=$tableid\\\\&_name\\\\=name\\\\&loctype\\\\=2\\\\&_address\\\\=address\\\\&longitude\\\\=longitude\\\\&latitude\\\\=latitude
echo " "
sleep 60
done

注意sleep 60,沒有這個的話,可能無法上傳成功,至少我沒加sleep就全部請求返回失敗。

2.客戶端檢索和展示:

這里需要實現(xiàn)的是周邊檢索,周邊檢索的請求參數(shù)對象為 AMapCloudPOIAroundSearchRequest ,其中:center 參數(shù)為必填參數(shù)。

- (void)searchCloudMapWithCenterLocationCoordinate2D:(CLLocationCoordinate2D )coordinate {
    
    NSString *tableID = self.tableID  // 云圖的tableID
       
    AMapCloudPOIAroundSearchRequest *request = [[AMapCloudPOIAroundSearchRequest alloc] init];
    request.tableID = tableID;
    request.center = [AMapGeoPoint locationWithLatitude:coordinate.latitude longitude:coordinate.longitude];
    request.radius = 100000;
    request.offset = 100;  // 最多只能獲取100條數(shù)據(jù)
    request.page = 1;  // 第一頁
    
    [self.searchAPI AMapCloudPOIAroundSearch:request];      
}

//實現(xiàn)云檢索對應(yīng)的回調(diào)函數(shù),添加標(biāo)注Annotation
#pragma mark - AMapSearchDelegate
- (void)onCloudSearchDone:(AMapCloudSearchBaseRequest *)request response:(AMapCloudPOISearchResponse *)response {

    NSMutableArray *annotations = [[NSMutableArray alloc] init];
    for (AMapCloudPOI *poi in response.POIs) {
        AMapGeoPoint *point = poi.location;
        MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
        annotation.coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude);
        [annotations addObject:a1];
    }
   
    [self.mapView addAnnotations:[annotations copy]];
    [self.mapView showAnnotations:annotations animated:YES];
}

實現(xiàn) MAMapViewDelegate 協(xié)議中的 mapView:viewForAnnotation:回調(diào)函數(shù),設(shè)置標(biāo)注樣式。

- (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.image = [UIImage imageNamed:@"location_annotation"];
        }
       
        return annotationView;
    }
    return nil;
}

地圖定位加標(biāo)注的功能就完成了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容