今回の内容
- 緯度経度から住所などに変換
コードと簡単解説
-
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {}
内でlocations
から緯度と経度を取得します。 - 取得してきた緯度と経度から
reverseGeocodeLocation
により住所などに変換して利用することができる。
//使用者の現在地の緯度と経度を取得して、住所か建物の名前などに変換
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: (locations.first?.coordinate.latitude)!, longitude: (locations.first?.coordinate.longitude)!)) { placeMark, error in
if error != nil{
return
}
if let resultPlaceMark = placeMark?.first{
if resultPlaceMark.administrativeArea != nil || resultPlaceMark.locality != nil{
self.currentLocationLabel.text = resultPlaceMark.name! + resultPlaceMark.administrativeArea!
}else{
self.currentLocationLabel.text = resultPlaceMark.name!
}
}
}
}
終わり
ご指摘、ご質問などありましたら、コメントまでお願い致します。