2016-11-22 16 views
6

Khi tôi cố gắng sử dụng mã hóa địa lý đảo ngược, thông báo lỗi này xuất hiện.Lỗi CLGeocoder. GEOErrorDomain Code = -3

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

Mã của tôi là dưới đây:

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

này chỉ hoạt động bất cứ lúc nào, và tôi yêu cầu reverseGeocode vài lần mỗi giây. Vì vậy, tôi đoán thông báo lỗi này có liên quan đến giới hạn yêu cầu hoặc một cái gì đó? Có tài liệu nào về yêu cầu mã địa lý của Apple không? Cảm ơn bạn đã tạm ứng.


Cập nhật

đây là toàn bộ mã của tôi yêu cầu

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

bạn đã giải quyết vấn đề này chưa? –

+1

@RayTso Không, chúng tôi không thể. Dường như lỗi xảy ra khi chúng tôi yêu cầu nhiều yêu cầu trong một khoảng thời gian ngắn. Thay vào đó, chúng tôi tránh sự cố này bằng cách sử dụng cập nhật hàng đợi và cập nhật. –

Trả lời

7

Sau khi tìm kiếm ở mọi nơi để tìm câu trả lời trong tài liệu của Apple! :/

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.

Khi kiểm tra mã lỗi trong xử lý hoàn thành nó thực sự là Mạng Lỗi: 2

Hy vọng điều này sẽ giúp người!

+0

Số lượng yêu cầu tối đa là bao nhiêu? – Ricardo

+1

@Ricardo Số này không được tiết lộ bởi Apple. Các chi tiết duy nhất họ đưa ra ở trên trong báo giá. Tôi không nghĩ rằng có số lượng yêu cầu tối đa nhưng có giới hạn về việc bạn có thể đệ quy các yêu cầu đó một cách đệ quy nhanh như thế nào. –

-1

Bạn có vui chơi giải trí CLGeocoder(). Ví dụ, tôi có một vòng lặp kiểm tra thông qua một mảng tọa độ, nếu tôi nhưng CLGeocoder bên trong vòng lặp, tôi nhận được thông báo lỗi giống như bạn.

Hy vọng điều này sẽ hữu ích.

+0

Cảm ơn bạn đã trả lời nhưng, tôi sử dụng biến tĩnh cho CLGeocoder. nó không hoạt động. –

+0

Bạn có thể đăng mã của mình không? – AlexanderKaran

+0

Tôi đã cập nhật mã của mình. Cảm ơn bạn. –

Các vấn đề liên quan