Skip to content

Instantly share code, notes, and snippets.

@mnaruse
Last active July 17, 2020 15:14
Show Gist options
  • Save mnaruse/7092bd157497ca4c2f48ac058cebc9b4 to your computer and use it in GitHub Desktop.
Save mnaruse/7092bd157497ca4c2f48ac058cebc9b4 to your computer and use it in GitHub Desktop.
Swift標準のCLGeocoderを使ったジオコーディングと逆ジオコーディング
//
// GeocoderService.swift
// myGeocoding
//
// Created by Miharu Naruse on 2020/05/20.
// Copyright © 2020 Miharu Naruse. All rights reserved.
//
// https://developer.apple.com/documentation/corelocation/converting_between_coordinates_and_user-friendly_place_names
// https://developer.apple.com/documentation/corelocation/clplacemark
// https://developer.apple.com/documentation/corelocation/clgeocoder
//
import Foundation
import CoreLocation
/// 地理座標と地名の間で変換するためのインターフェースCLGeocoderを使用したクラス
final class GeocoderService {
/// 座標の逆ジオコーディング
/// - Parameters:
/// - lastLocation: 最後に報告された位置情報
/// - completionHandler: 完了ハンドラー
func lookUpCurrentLocation(lastLocation: CLLocation?, completionHandler: @escaping (CLPlacemark?) -> Void) {
// Use the last reported location.
if let lastLocation = lastLocation {
let geocoder = CLGeocoder()
// Look up the location and pass it to the completion handler
geocoder.reverseGeocodeLocation(lastLocation,
completionHandler: { (placemarks, error) in
if error == nil {
let firstLocation = placemarks?[0]
completionHandler(firstLocation)
}
else {
// An error occurred during geocoding.
completionHandler(nil)
}
})
}
else {
// No location was available.
completionHandler(nil)
}
}
/// 目印を座標に変換する。
/// - Parameters:
/// - addressString: 住所の文字列
/// - completionHandler: 完了ハンドラー
func getCoordinate(addressString: String,
completionHandler: @escaping(CLLocationCoordinate2D, NSError?) -> Void) {
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(addressString) { (placemarks, error) in
if error == nil {
if let placemark = placemarks?[0] {
let location = placemark.location!
completionHandler(location.coordinate, nil)
return
}
}
completionHandler(kCLLocationCoordinate2DInvalid, error as NSError?)
}
}
/// おまけ
/// - Parameter placemark: 目印
func printCurrentLocationPlacemark(_ placemark: CLPlacemark?) {
guard let placemark = placemark else { return }
/// 目印の名前。
var name: String?
/// 省略された国名。
var isoCountryCode: String?
/// 目印に関連付けられている国の名前。
var country: String?
/// 目印に関連付けられている郵便番号。
var postalCode: String?
/// 目印に関連付けられている都道府県。
var administrativeArea: String?
/// 目印の追加の行政区域情報。
var subAdministrativeArea: String?
/// 目印に関連付けられている都市。
var locality: String?
/// 目印の都市レベルの追加情報。
var subLocality: String?
/// 目印に関連付けられた住所。
var thoroughfare: String?
/// 目印の番地レベルの追加情報。
var subThoroughfare: String?
/// 目印に関連付けられている地域。
var region: CLRegion?
/// 目印に関連付けられているタイムゾーン。
var timeZone: TimeZone?
name = placemark.name
isoCountryCode = placemark.isoCountryCode
country = placemark.country
postalCode = placemark.postalCode
administrativeArea = placemark.administrativeArea
subAdministrativeArea = placemark.subAdministrativeArea
locality = placemark.locality
subLocality = placemark.subLocality
thoroughfare = placemark.thoroughfare
subThoroughfare = placemark.subThoroughfare
region = placemark.region
timeZone = placemark.timeZone
print("\(name)")
print("\(isoCountryCode)")
print("\(country)")
print("\(postalCode)")
print("\(administrativeArea)")
print("\(subAdministrativeArea)")
print("\(locality)")
print("\(subLocality)")
print("\(thoroughfare)")
print("\(subThoroughfare)")
print("\(region)")
print("\(timeZone)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment