如何使用CoreLocation快速从英国邮政编码中获取自治市镇? [英] How to get borough from UK postcode using CoreLocation in swift?
问题描述
有人知道我是否可以使用CoreLocation框架从英国邮政编码中获得自治市镇?在下面列出的函数中,不返回自治市镇。它应该返回Tower Hamlets。
Does anyone know if I can get the borough from a UK post code using CoreLocation framework? In the function listed below, the borough is not returned. It should return Tower Hamlets.
func getLocationFrom(postalCode : String){
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(postalCode) {
(placemarks, error) -> Void in
let placemark = placemarks?[0]
print("placemark.addressDictionary are \(placemark.addressDictionary)") //does not print Canary Wharf
}
}
print(getLocationFrom(postalCode:"E14 9LR")) //does not print Tower Hamlets, it prints London, which is the city, not the borough..
推荐答案
为了从邮政编码中获取位置信息,您只需下标地标
数组,然后访问所需indexPath中元素的属性 locality
。我的错误是我尝试访问地标
数组上的另一个属性,即 addressDictionary
,该属性未产生预期的结果。
In order to get the locality from a postcode you just have to subscript placemarks
array and then access the property locality
for the element at the indexPath you want. My mistake was that I was trying to access another property on the placemarks
array, namely addressDictionary
which was not producing the expected result.
func getLocationFrom(postalCode: String, callback: @escaping(_ locality: String?) -> Void){
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(postalCode) {
(placemarks, error) -> Void in
callback(placemarks?[0].locality)
}
这篇关于如何使用CoreLocation快速从英国邮政编码中获取自治市镇?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!