1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOS 11 の CLGeocoder geocodePostalAddress を試す

Posted at

iOS 11 で追加される CLGeocodergeocodePostalAddress を試してみました。既存の 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

参考

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?