[swift] CLGeocoderの逆Geoコーディングで位置座標から住所等の情報取得

目次

CLGeocoderの逆Geoコーディングで位置座標から住所等の情報取得

CLGeocoder().reverseGeocodeLocationメソッドを使用することで、指定の位置座標から住所や国名などの地理情報を取得できる

返される情報はデバイスの言語に依存する様子

シュミレータは英語仕様だったので、英語で返ってくる

サンプルコード:

AppDelegate

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        getPlaceMarks(locations)
    }

    func displayLocationInfo(placemark: CLPlacemark){
        
        print("location is \(placemark.location)")
        print("addressDictionary is \(placemark.addressDictionary)")
        print("ISOcountryCode is \(placemark.ISOcountryCode)")
        print("country is \(placemark.country)")
        print("postalCode is \(placemark.postalCode)")
        print("administrativeArea is \(placemark.administrativeArea)")
        print("subAdministrativeArea is \(placemark.subAdministrativeArea)")
        print("locality is \(placemark.locality)")
        print("subLocality is \(placemark.subLocality)")
        print("thoroughfare is \(placemark.thoroughfare)")
        print("subThoroughfare is \(placemark.subThoroughfare)")
        print("region is \(placemark.region)")
        print("timeZone is \(placemark.timeZone)")
        print("inlandWater is \(placemark.inlandWater)")
        print("ocean is \(placemark.ocean)")
        print("areasOfInterest is \(placemark.areasOfInterest)")
        
    }
    
    func getPlaceMarks(location: CLLocation){
        CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error)->Void in
            if error != nil {
                print(error!.localizedDescription)
                return
            }
            if placemarks!.count > 0 {
                let placemark = placemarks![0] as CLPlacemark
                self.displayLocationInfo(placemark)
                
            } else {
                print("error")
            }
        })
    }

要素の詳細

それぞれの要素の詳細は以下の通り

location: 取得したCLLocation

addressDictionary :[NSObject:AnyObject]型で一連の住所等の情報が返ってくる、連結した住所も返ってくる様子

ISOcountryCode :省略された国名

country :国名

postalCode :郵便番号

administrativeArea :州名

subAdministrativeArea :州の追加情報

locality :都市名

subLocality :都市の追加情報

thoroughfare :通り名

subThoroughfare :通りの追加情報

region :CLRegion型で返ってくる領域情報

timeZone :TimeZone型で返ってくるタイムゾーン情報

inlandWater :公式ページには「inland water body(内面水)」とあったが湖や川?

ocean :海名

areasOfInterest :著名なLandMarkのよう

シュミレータでの結果:

*位置情報はApple本社

location is Optional(<+37.33004350,-122.02647140> +/- 100.00m (speed -1.00 mps / course -1.00) @ 1/12/17, 7:07:57 AM Japan Standard Time)

addressDictionary is Optional([SubAdministrativeArea: Santa Clara, CountryCode: US, Street: 20204 Merritt Dr, State: CA, ZIP: 95014, Name: 20204 Merritt Dr, Thoroughfare: Merritt Dr, FormattedAddressLines: <__NSArrayM 0x60000024be80>(
20204 Merritt Dr,
Cupertino, CA  95014,
United States
)
, SubThoroughfare: 20204, PostCodeExtension: 2011, Country: United States, City: Cupertino])

ISOcountryCode is Optional("US")

country is Optional("United States")

postalCode is Optional("95014")

administrativeArea is Optional("CA")

subAdministrativeArea is Optional("Santa Clara")

locality is Optional("Cupertino")

subLocality is nil

thoroughfare is Optional("Merritt Dr")

subThoroughfare is Optional("20204")

region is Optional(CLCircularRegion (identifier:'<+37.33004350,-122.02647140> radius 70.86', center:<+37.33004350,-122.02647140>, radius:70.86m))

timeZone is Optional(America/Los_Angeles (PST) offset -28800)

inlandWater is nil

ocean is nil

areasOfInterest is nil

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です