LoginSignup
61
54

More than 1 year has passed since last update.

[Swift] iOSの標準ライブラリだけで、郵便番号から住所を取得する

Last updated at Posted at 2015-11-27

概要

CoreLocationライブラリを使って、郵便番号から住所を取得することができます。(注:インターネット接続が必要です)

Solution

ソースコード

//: Playground - noun: a place where people can play

import UIKit
import CoreLocation
import XCPlayground

let geocoder = CLGeocoder()
geocoder.geocodeAddressString("〒100-0002", 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!)")
  }
})

XCPSetExecutionShouldContinueIndefinitely()

playgroundでの実行結果

レスポンスが英語になります

State:       Tokyo
City:        Chiyoda
SubLocality: Kokyogaien

iPhoneでの実行結果

レスポンスが日本語になりました。端末の言語設定に依存するようです。
ちなみに、機内モードで実行するとエラーになります。

State:       東京都
City:        千代田区
SubLocality: 皇居外苑

Androidの場合

以下のAPIで同じようなレスポンスが返ってきた


{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "100-0002",
               "short_name" : "100-0002",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "皇居外苑",
               "short_name" : "皇居外苑",
               "types" : [ "sublocality_level_1", "sublocality", "political" ]
            },
            {
               "long_name" : "千代田区",
               "short_name" : "千代田区",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "東京都",
               "short_name" : "東京都",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "日本",
               "short_name" : "JP",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "〒100-0002, 日本",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 35.6844641,
                  "lng" : 139.7626249
               },
               "southwest" : {
                  "lat" : 35.6753588,
                  "lng" : 139.7532534
               }
            },
            "location" : {
               "lat" : 35.6796282,
               "lng" : 139.7588189
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 35.6844641,
                  "lng" : 139.7626249
               },
               "southwest" : {
                  "lat" : 35.6753588,
                  "lng" : 139.7532534
               }
            }
         },
         "place_id" : "ChIJhfpjafGLGGARU6M89KmiydY",
         "types" : [ "postal_code" ]
      }
   ],
   "status" : "OK"
}

参考

61
54
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
61
54