【问题标题】:CLGeocoder error. GEOErrorDomain Code=-3CLG 编码器错误。 GEOErrorDomain 代码=-3
【发布时间】:2016-11-22 06:36:46
【问题描述】:

当我尝试使用反向地理编码时,出现此错误消息。

地理编码错误:Error Domain=GEOErrorDomain Code=-3 "(null)"

我的代码如下:

import CoreLocation
geocoder.reverseGeocodeLocation(location) { (placemarks, error) in
    if let placemarks = placemarks {
        reverseGeocodeLocations[hash] = placemarks
    }
    callback(placemarks, error)
}

这只是有时有效,我每秒请求 reverseGeocode 几次。所以我猜这个错误信息与请求的限制有关吗? 有没有关于苹果地理编码请求的文档? 感谢您的提前。


更新

这是我的整个请求代码

import CoreLocation

fileprivate struct ReverseGeocodeRequest {

    private static let geocoder = CLGeocoder()
    private static var reverseGeocodeLocations = [Int: [CLPlacemark]]()
    private static let reverseGeocodeQueue = DispatchQueue(label: "ReverseGeocodeRequest.reverseGeocodeQueue")

    private static var nextPriority: UInt = 0

    fileprivate static func request(location: CLLocation, callback: @escaping ([CLPlacemark]?, Error?)->Void) {
        let hash = location.hash
        if let value = reverseGeocodeLocations[hash] {
            callback(value, nil)
        } else {
            reverseGeocodeQueue.async {
                guard let value = reverseGeocodeLocations[hash] else {
                    geocoder.reverseGeocodeLocation(location) { (placemarks, error) in
                        if let placemarks = placemarks {
                            reverseGeocodeLocations[hash] = placemarks
                        }
                        callback(placemarks, error)
                    }
                    return
                }
                callback(value, nil)
            }
        }
    }



    let priority: UInt
    let location: CLLocation
    let handler : ([CLPlacemark]?, Error?)->Void

    private init (location: CLLocation, handler: @escaping ([CLPlacemark]?, Error?)->Void) {
        ReverseGeocodeRequest.nextPriority += 1
        self.priority = ReverseGeocodeRequest.nextPriority
        self.location = location
        self.handler  = handler
    }

}

extension ReverseGeocodeRequest: Comparable {
    static fileprivate func < (lhs: ReverseGeocodeRequest, rhs: ReverseGeocodeRequest) -> Bool {
        return lhs.priority < rhs.priority
    }
    static fileprivate func == (lhs: ReverseGeocodeRequest, rhs: ReverseGeocodeRequest) -> Bool {
        return lhs.priority == rhs.priority
    }

}


extension CLLocation {

    func reverseGeocodeLocation(callback: @escaping ([CLPlacemark]?, Error?)->Void) {
        ReverseGeocodeRequest.request(location: self, callback: callback)
    }

    func getPlaceName(callback: @escaping (Error?, String?)->Void) {
        self.reverseGeocodeLocation { (placemarks, error) in
            guard let placemarks = placemarks, error == nil else {
                callback(error, nil)
                return
            }
            guard let placemark = placemarks.first else {
                callback(nil, "Mysterious place")
                return
            }

            if let areaOfInterest = placemark.areasOfInterest?.first {
                callback(nil, areaOfInterest)
            } else if let locality = placemark.locality {
                callback(nil, locality)
            } else {
                callback(nil, "On the Earth")
            }
        }
    }
}

【问题讨论】:

  • 你解决了吗?
  • @RayTso 不,我们不能。当我们在短时间内询问多个请求时,似乎会发生错误。相反,我们使用排队和延迟更新来避免这个问题。

标签: ios clgeocoder


【解决方案1】:

在到处寻找答案后,它是在 Apples 文档中! :/

https://developer.apple.com/reference/corelocation/clgeocoder/1423621-reversegeocodelocation

每个应用的地理编码请求都有速率限制,因此在短时间内发出过多请求可能会导致某些请求失败。当超过最大速率时,地理编码器将带有值网络的错误对象传递给您的完成处理程序。

在完成处理程序中检查错误代码时,确实是 Network Error:2

希望这对某人有所帮助!

【讨论】:

  • 最大请求数是多少?
  • @Ricardo Apple 没有透露这个数字。他们给出的唯一细节在上面的报价中。我认为没有请求的最大数量,但递归执行这些请求的速度是有限制的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-25
  • 1970-01-01
  • 2013-07-25
  • 2015-01-29
  • 1970-01-01
  • 1970-01-01
  • 2018-04-12
相关资源
最近更新 更多