ios – CLGeocoder错误. GEOErrorDomain代码= -3

前端之家收集整理的这篇文章主要介绍了ios – CLGeocoder错误. GEOErrorDomain代码= -3前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我尝试使用反向地理编码时,出现此错误消息.

Geocode error: Error Domain=GEOErrorDomain Code=-3 “(null)”

我的代码如下:

  1. import CoreLocation
  2. geocoder.reverseGeocodeLocation(location) { (placemarks,error) in
  3. if let placemarks = placemarks {
  4. reverseGeocodeLocations[hash] = placemarks
  5. }
  6. callback(placemarks,error)
  7. }

这只是不时工作,我每秒多次请求reverseGeocode.所以我猜这个错误信息与请求的限制有什么关系?
有没有关于apple的地理编码请求的文档?
谢谢你提前.

更新

这是我的整个代码请求

  1. import CoreLocation
  2.  
  3. fileprivate struct ReverseGeocodeRequest {
  4.  
  5. private static let geocoder = CLGeocoder()
  6. private static var reverseGeocodeLocations = [Int: [CLPlacemark]]()
  7. private static let reverseGeocodeQueue = DispatchQueue(label: "ReverseGeocodeRequest.reverseGeocodeQueue")
  8.  
  9. private static var nextPriority: UInt = 0
  10.  
  11. fileprivate static func request(location: CLLocation,callback: @escaping ([CLPlacemark]?,Error?)->Void) {
  12. let hash = location.hash
  13. if let value = reverseGeocodeLocations[hash] {
  14. callback(value,nil)
  15. } else {
  16. reverseGeocodeQueue.async {
  17. guard let value = reverseGeocodeLocations[hash] else {
  18. geocoder.reverseGeocodeLocation(location) { (placemarks,error) in
  19. if let placemarks = placemarks {
  20. reverseGeocodeLocations[hash] = placemarks
  21. }
  22. callback(placemarks,error)
  23. }
  24. return
  25. }
  26. callback(value,nil)
  27. }
  28. }
  29. }
  30.  
  31.  
  32.  
  33. let priority: UInt
  34. let location: CLLocation
  35. let handler : ([CLPlacemark]?,Error?)->Void
  36.  
  37. private init (location: CLLocation,handler: @escaping ([CLPlacemark]?,Error?)->Void) {
  38. ReverseGeocodeRequest.nextPriority += 1
  39. self.priority = ReverseGeocodeRequest.nextPriority
  40. self.location = location
  41. self.handler = handler
  42. }
  43.  
  44. }
  45.  
  46. extension ReverseGeocodeRequest: Comparable {
  47. static fileprivate func < (lhs: ReverseGeocodeRequest,rhs: ReverseGeocodeRequest) -> Bool {
  48. return lhs.priority < rhs.priority
  49. }
  50. static fileprivate func == (lhs: ReverseGeocodeRequest,rhs: ReverseGeocodeRequest) -> Bool {
  51. return lhs.priority == rhs.priority
  52. }
  53.  
  54. }
  55.  
  56.  
  57. extension CLLocation {
  58.  
  59. func reverseGeocodeLocation(callback: @escaping ([CLPlacemark]?,Error?)->Void) {
  60. ReverseGeocodeRequest.request(location: self,callback: callback)
  61. }
  62.  
  63. func getPlaceName(callback: @escaping (Error?,String?)->Void) {
  64. self.reverseGeocodeLocation { (placemarks,error) in
  65. guard let placemarks = placemarks,error == nil else {
  66. callback(error,nil)
  67. return
  68. }
  69. guard let placemark = placemarks.first else {
  70. callback(nil,"MysterIoUs place")
  71. return
  72. }
  73.  
  74. if let areaOfInterest = placemark.areasOfInterest?.first {
  75. callback(nil,areaOfInterest)
  76. } else if let locality = placemark.locality {
  77. callback(nil,locality)
  78. } else {
  79. callback(nil,"On the Earth")
  80. }
  81. }
  82. }
  83. }

解决方法

搜索到答案后,它在Apples文档中! :/

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

Geocoding requests are rate-limited for each app,so making too many requests in a short period of time may cause some of the requests to fail. When the maximum rate is exceeded,the geocoder passes an error object with the value network to your completion handler.

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

希望这有助于某人!

猜你在找的iOS相关文章