无法在swift中使用CLPlacemark获得次定位

无法在swift中使用CLPlacemark获得次定位,swift,swift3,clplacemark,Swift,Swift3,Clplacemark,我实际上正在用swift构建一个应用程序。 问题是应用程序应该从经纬度获取子局部性,并检查该子局部性是否是数组的一部分,但我无法获取子局部性 这里的专业人士能帮我吗 这是使用的经纬度 let loc: CLLocation = CLLocation(latitude:43.60089111328125, longitude: 3.875329601741884) ceo.reverseGeocodeLocation(loc, completionHandler: {(placemarks, e

我实际上正在用swift构建一个应用程序。 问题是应用程序应该从经纬度获取子局部性,并检查该子局部性是否是数组的一部分,但我无法获取子局部性

这里的专业人士能帮我吗

这是使用的经纬度

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作为子局部性,但没有任何结果。

您可以使用
地理编码器
根据位置坐标进行反向地理编码,该位置坐标将返回
CLPlacemark
,然后您可以检查
子局部性
,如果存在,如下所示:

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
    }
})

你能试试吗,它会帮你的

   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)")
            }

     })

这可能会有帮助:“什么都不会发生”,因为你不是在要求亚焦度。