iOS 11 で追加される CLGeocoder
の geocodePostalAddress
を試してみました。既存の reverseGeocodeLocation
と合わせて、住所と緯度経度とが相互に変換できるようになりそうです。
コード
//: Playground - noun: a place where people can play
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
import UIKit
import CoreLocation // CLLocation, CLGeocoder
import Contacts // CNPostalAddress
// lat_long -> address
var cur = CLLocation(latitude: 35.66, longitude:139.75);
CLGeocoder().reverseGeocodeLocation(cur, completionHandler:
{(placemarks, error) -> Void in
if((error) != nil){
print("Error", error!)
}
if let placemark = placemarks?.first {
print("State: \(placemark.administrativeArea!)")
print("City: \(placemark.locality!)")
print("SubLocality: \(placemark.subLocality!)")
}
})
// address -> lat_long
var postal = CNMutablePostalAddress()
postal.state = "Tokyo"
postal.city = "Minato"
postal.subLocality = "Shibadaimon"
CLGeocoder().geocodePostalAddress(postal, completionHandler: {(placemarks, error) -> Void in
if((error) != nil){
print("Error", error!)
}
if let placemark = placemarks?.first {
print("latitude: \(placemark.location!.coordinate.latitude)")
print("longitude: \(placemark.location!.coordinate.longitude)")
}
})
playground での実行結果
State: Tokyo
City: Minato
SubLocality: Shibakoen
latitude: 35.6600347
longitude: 139.7538893