问题
I have an app that needs to convert many addresses to CLPlacemark to get the latitude/longitude. In the Apple documentation it's explained that we should not perform many GeoCode request, Geocoding requests are rate limited.
In my App, to perform the Geocoding I just request one Address and when I get the answer I will request the next one, until I have received the lat/long for all my addresses.
I will put this code in a dedicated thread to not block the user. It seems to work but is it a good strategy? How can I be sure Apple will not blacklist my App or return error for my requests.
geoCodingFor(myAddress, index: 0)
private func geoCodingFor(contacts:[String], index:Int) {
if index < contacts.count {
let address = contacts[index]
CLGeocoder().geocodeAddressString(address) { placemarks, error in
if let theError = error {
print("\(#function) geocode has failed for address \(address) with error \(theError .localizedDescription)")
} else {
if let thePlacemark = placemarks {
if thePlacemark.count > 0 {
print("=> Found placemark for address \(address)")
} else {
print("No placemark for address \(address)")
}
} else {
print("\(#function) Warning geocode has no results for address \(address)")
}
}
if index < contacts.count {
self.geoCodingFor(contacts, index: index + 1)
}
}
} else {
print("\(#function) ================> Geocoding is completed")
}
}
回答1:
Short: Maybe just try sleep(2)
before each geocoding request.
Long: While one cannot be certain if Apple would 'blacklist' their app. I am not sure 'blacklisting' app for geocoding a lot exists.
I myself have tried a similar strategy to yours. Only used outer variable queue
and perform queue.popLast()
. Doing this gave me like a 100 results and then I started getting an error for the rest of the addresses.
I have added a sleep(2)
before each geocoding request and I managed to extract more than 4000 geocodings with no blocking.
The downside is - it takes more time. Depending on how many addresses you are planning to geocode this might be useful.
Also please note that I never tried sleep(1)
, which could also work to some extent.
According to Apple's docs 'maximum rate' is not specified and they recommend that
you should not send more than one geocoding request per minute
I hope this helps.
Cheers.
来源:https://stackoverflow.com/questions/36619134/strategy-to-perform-many-geocode-requests