繁体   English   中英

通过邮政编码获取位置时,Domain=kCLErrorDomain Code=8

[英]Domain=kCLErrorDomain Code=8 when fetching location through zipcode

我正在尝试根据输入参数邮政/城市和国家/地区代码获取纬度和经度。 下面是我的代码,如果输入城市和国家名称,这可以正常工作,但如果我输入邮政编码和国家代码,则显示错误。 下面是代码。 (注意:启用位置服务和应用程序权限)

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,CN

编辑

按照答案中的建议附加了更新的代码,但仍然遇到同样的问题

在此处输入图像描述

目前,您在设置之前使用return location ,因为 geocoder.geocodePostalAddress geocoder.geocodePostalAddress(...)是一个异步 function。

这意味着您需要使用完成处理程序(例如)返回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)
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM