ios高德地圖,地理編碼,基於swift 4.0
阿新 • • 發佈:2019-02-14
最近在做專案中,需要用到高德地圖的,地理編碼的功能。但是搜了一下部落格,都是反地理的編碼的文章。而高德的文件,都藏的比較深。下面就直接寫程式碼了。
import UIKit class AmpSearchManager: NSObject { static let sharedInstance = AmpSearchManager() var request:AMapGeocodeSearchRequest=AMapGeocodeSearchRequest() var callback: ((AMapGeocode)->())? let search = { () -> AMapSearchAPI? in let serarch = AMapSearchAPI() return serarch }() func getGeocode(address:String,callback:((AMapGeocode)->())?){ //AMapSearchServices.shared().apiKey = "您的Key" AMapServices.shared().apiKey = gaoDeKey self.search?.delegate = self request.address = address self.callback = callback self.search?.aMapGeocodeSearch(request) } } extension AmpSearchManager:AMapSearchDelegate{ func onGeocodeSearchDone(_ request: AMapGeocodeSearchRequest!, response: AMapGeocodeSearchResponse!) { if response.geocodes == nil { return } callback!(response.geocodes[0]) // print(response.geocodes.count) // for geo in response.geocodes { // // print(geo.building) // print(geo.formattedAddress) // print(geo.location) // // } //解析response獲取地理資訊,具體解析見 Demo } }
程式碼就這麼多行,怎麼呼叫呢。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { print("______________") // self.navigationController?.pushViewController(EditHotelViewController(), animated: true) AmpSearchManager.sharedInstance.getGeocode(address: "深圳錦華大廈", callback: { a in }) }
現在我們來看AMapGeocode裡面有一些什麼屬性呢。
///地理編碼 @interface AMapGeocode : AMapSearchObject ///格式化地址 @property (nonatomic, copy) NSString *formattedAddress; ///所在省/直轄市 @property (nonatomic, copy) NSString *province; ///城市名 @property (nonatomic, copy) NSString *city; ///城市編碼 @property (nonatomic, copy) NSString *citycode; ///區域名稱 @property (nonatomic, copy) NSString *district; ///區域編碼 @property (nonatomic, copy) NSString *adcode; ///鄉鎮街道 @property (nonatomic, copy) NSString *township; ///社群 @property (nonatomic, copy) NSString *neighborhood; ///樓 @property (nonatomic, copy) NSString *building; ///座標點 @property (nonatomic, copy) AMapGeoPoint *location; ///匹配的等級 @property (nonatomic, copy) NSString *level; @end