iOS關於高德SDK詳解和簡單使用

來源:互聯網
上載者:User

標籤:

 根據現在項目需求,地圖,定位成了現在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官方文檔。

 

 

 

 

 

 

 

 

  

 

iOS關於高德SDK詳解和簡單使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.