LoginSignup
18
18

More than 5 years have passed since last update.

CLGeocoderを使って逆ジオコーディング

Posted at

緯度経度から住所を文字情報で取得する。

コード

func revGeocoding(coordinate: CLLocationCoordinate2D)
{
    let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
    var geocoder = CLGeocoder()
    geocoder.reverseGeocodeLocation(location, completionHandler: { (placemarks:[AnyObject]!, error:NSError!) -> Void in
        if (error == nil && placemarks.count > 0) {
            let placemark = placemarks[0] as CLPlacemark
            println("Country = \(placemark.country)")
            println("Postal Code = \(placemark.postalCode)")
            println("Administrative Area = \(placemark.administrativeArea)")
            println("Sub Administrative Area = \(placemark.subAdministrativeArea)")
            println("Locality = \(placemark.locality)")
            println("Sub Locality = \(placemark.subLocality)")
            println("Throughfare = \(placemark.thoroughfare)")
        } else if (error == nil && placemarks.count == 0) {
            println("No results were returned.")
        } else if (error != nil) {
            println("An error occured = \(error.localizedDescription)")
        }
    })
}

CLPlacemarkに入っている情報

  • Administrative Area
    • 都道府県
  • Locality
    • 区や市名
  • Sub Locality
    • さらに細かい地域名

などなど。
端末の設定言語で取得される。

18
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
18