无法在 swift 中使用 CLPlacemark 获取 subLocality
Posted
技术标签:
【中文标题】无法在 swift 中使用 CLPlacemark 获取 subLocality【英文标题】:Not able to get subLocality using CLPlacemark in swift 【发布时间】:2019-04-12 14:20:10 【问题描述】:我实际上正在快速构建一个应用程序。 问题是应用程序应该从经度纬度获取子位置并检查该子位置是否是数组的一部分,但我无法获得子位置
这里的任何专业人士都可以帮助我吗
这是使用的经度纬度
let loc: CLLocation = CLLocation(latitude:43.60089111328125, longitude: 3.875329601741884)
ceo.reverseGeocodeLocation(loc, completionHandler: (placemarks, error) in
if (error != nil)
print("reverse geodcode fail: \(error!.localizedDescription)")
let pm = placemarks! as [CLPlacemark]
if pm.count > 0
let pm = placemarks![0]
addressString = ""
if pm.name != nil
addressString += pm.name! + ", "
if pm.isoCountryCode != nil
addressString += pm.isoCountryCode! + ", "
if pm.country != nil
addressString += pm.country! + ", "
if pm.postalCode != nil
addressString += pm.postalCode! + ", "
if pm.administrativeArea != nil
addressString += pm.administrativeArea! + ", "
if pm.subAdministrativeArea != nil
addressString += pm.subAdministrativeArea! + ", "
if pm.locality != nil
addressString += pm.locality! + ", "
if pm.subLocality != nil
addressString += pm.subLocality! + ", "
if pm.thoroughfare != nil
addressString += pm.thoroughfare! + ", "
if pm.subThoroughfare != nil
addressString += pm.subThoroughfare! + ", "
if pm.areasOfInterest != nil
print(pm.areasOfInterest!)
print(pm.addressDictionary)
)
应该将 GAMBETTA 作为 subLocality 返回,但什么都不会发生。
【问题讨论】:
这可能会有所帮助:***.com/questions/16647996/… “什么都不会发生”,因为您没有要求子地区性。 【参考方案1】:您可以根据位置坐标使用Geocoder
进行反向地理编码,这将返回CLPlacemark
,然后您可以检查subLocality
(如果存在),如下所示:
let geoCoder = CLGeocoder()
geoCoder.reverseGeocodeLocation(loc, completionHandler: placemarks, error in
// Place details
guard let placeMark = placemarks?.first else return
// SubLocality
if let subLocality = placeMark.subLocality
// Do something if exists
)
【讨论】:
【参考方案2】:你能不能试试,对你有帮助。
let newlocation: CLLocation = CLLocation(latitude:43.60089111328125, longitude: 3.875329601741884)
let geoCoder = CLGeocoder()
geoCoder.reverseGeocodeLocation(newlocation, completionHandler: (placemarks, error) -> Void in
// Place details
var placeMark: CLPlacemark!
placeMark = placemarks?[0]
print(placeMark.subLocality as Any)
// Address dictionary
print(placeMark.addressDictionary as Any)
//City
if let city = placeMark.addressDictionary!["City"] as? String
print("city : \(city)")
)
【讨论】:
以上是关于无法在 swift 中使用 CLPlacemark 获取 subLocality的主要内容,如果未能解决你的问题,请参考以下文章
Swift 从 CLLocation 获取 CLPlacemark
Swift ReverseGeocode CLPlacemark areaOfInterest 几乎总是为零。我应该使用别的东西吗?
无法弄清楚如何使用 CLPlacemark 将我当前的位置正确嵌入到消息正文中