iOS 原生地圖地理編碼與反地理編碼,ios地圖編碼地理

來源:互聯網
上載者:User

iOS 原生地圖地理編碼與反地理編碼,ios地圖編碼地理

 

  當我們要在App實現功能:輸入地名,編碼為經緯度,實現導航功能。  那麼,我需要用到原生地圖中的地理編碼功能,而在Core Location中主要包含了定位、地理編碼(包括反編碼)功能。

 

 

在檔案中匯入

 

#import <CoreLocation/CoreLocation.h>

 

地理編碼:

/** 地理編碼 */- (void)geocoder {        CLGeocoder *geocoder=[[CLGeocoder alloc]init];        NSString *addressStr = @"廣東省深圳市寶安區";//位置資訊        [geocoder geocodeAddressString:addressStr completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {        if (error!=nil || placemarks.count==0) {            return ;        }        //建立placemark對象        CLPlacemark *placemark=[placemarks firstObject];        //經度        NSString *longitude =[NSString stringWithFormat:@"%f",placemark.location.coordinate.longitude];        //緯度        NSString *latitude =[NSString stringWithFormat:@"%f",placemark.location.coordinate.latitude];                NSLog(@"經度:%@,緯度:%@",longitude,latitude);            }];    }

 

 

 

地理反編碼:

 

/** 地理反編碼 */- (void)reverseGeocoder{    //建立地理編碼對象    CLGeocoder *geocoder=[[CLGeocoder alloc]init];        //經度    NSString *longitude = @"113.23";    //緯度    NSString *latitude = @"23.16";            //建立位置    CLLocation *location=[[CLLocation alloc]initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];            //反地理編碼    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {        //判斷是否有錯誤或者placemarks是否為空白        if (error !=nil || placemarks.count==0) {            NSLog(@"%@",error);            return ;        }        for (CLPlacemark *placemark in placemarks) {            //詳細地址            NSString *addressStr = placemark.name;            NSLog(@"詳細地址1:%@",addressStr);            NSLog(@"詳細地址2:%@",placemark.addressDictionary);            NSLog(@"詳細地址3:%@",placemark.locality);        }            }];}

 


Demo:https://github.com/JnKindle/GeocoderAndReverseReocoder

 

/**

 *  Author:Jn

 *  GitHub:https://github.com/JnKindle

 *  cnblogs:http://www.cnblogs.com/JnKindle

 *  QQ:1294405741

 */

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.