swift 通过邮政编码获取位置时,域= kCLEror域代码=8

axr492tv  于 2023-02-03  发布在  Swift
关注(0)|答案(1)|浏览(143)

我试图获取的纬度和经度的输入参数邮政/城市和国家代码的基础上。下面是我的代码,这工程罚款,如果输入城市和国家名称,但显示错误,如果我输入邮编和国家代码。下面是代码。(注:定位服务和应用程序权限已启用)

func getLocationFrom(postalCityCode: String, countryCode: String) -> CLLocation? {
        let geocoder = CLGeocoder()
        var location: CLLocation?
        let address = CNMutablePostalAddress()
        address.postalCode = postalCityCode
        address.country = countryCode
        geocoder.geocodePostalAddress(address, preferredLocale: Locale.current) { (placemarks, error) in
            guard error == nil else {
                print("Error: \(error!)")
                return
            }
            guard let placemark = placemarks?.first else {
                print("Error: placemark is nil")
                return
            }
            guard let coordinate = placemark.location?.coordinate else {
                print("Error: coordinate is nil")
                return
            }
            location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
            print("Found location = \(location)")
        }
        return location
    }

工作输入:中国上海
输入失败:200040,中文
编辑
随附了答案中建议的更新代码,但仍遇到相同问题

k3fezbri

k3fezbri1#

当前,您使用的是未设置的return location,因为geocoder.geocodePostalAddress(...)是异步函数。
这意味着你需要使用一个完成处理程序(例如)来返回location,当它有结果时,如下所示:

func getLocationFrom(postalCityCode: String, countryCode: String, completion: @escaping ( CLLocation?) -> Void) {
    
    let geocoder = CLGeocoder()
    var location: CLLocation?
    let address = CNMutablePostalAddress()
    address.postalCode = postalCityCode
    address.country = countryCode
    geocoder.geocodePostalAddress(address, preferredLocale: Locale.current) { (placemarks, error) in
        guard error == nil else {
            print("Error: \(error!)")
            return completion(nil)
        }
        guard let placemark = placemarks?.first else {
            print("Error: placemark is nil")
            return completion(nil)
        }
        guard let coordinate = placemark.location?.coordinate else {
            print("Error: coordinate is nil")
            return completion(nil)
        }
        location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
        print("\n--> Found location = \(location) \n")
        completion(location)   // <-- here
    }
}

像这样使用它:

getLocationFrom(postalCityCode: "200040", countryCode: "CN") { location in
     print("\n---> location: \(location) \n")
 }

编辑-1
为了测试和隔离问题,请在新的SwiftUI项目中尝试以下代码:

struct ContentView: View {
    @State var cityLocation = CLLocation()

    var body: some View {
        Text(cityLocation.description)
            .onAppear {
                getLocationFrom(postalCityCode: "200040", countryCode: "CN") { location in
                    print("\n---> location: \(location) \n")
                    if let theLocation = location {
                        cityLocation = theLocation
                    }                    
                }
            }
    }
    
    func getLocationFrom(postalCityCode: String, countryCode: String, completion: @escaping ( CLLocation?) -> Void) {
        
        let geocoder = CLGeocoder()
        var location: CLLocation?
        let address = CNMutablePostalAddress()
        address.postalCode = postalCityCode
        address.country = countryCode
        geocoder.geocodePostalAddress(address, preferredLocale: Locale.current) { (placemarks, error) in
            guard error == nil else {
                print("Error: \(error!)")
                return completion(nil)
            }
            guard let placemark = placemarks?.first else {
                print("Error: placemark is nil")
                return completion(nil)
            }
            guard let coordinate = placemark.location?.coordinate else {
                print("Error: coordinate is nil")
                return completion(nil)
            }
            location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
            print("\n--> Found location = \(location) \n")
            completion(location)
        }
    }
}

相关问题