【发布时间】:2016-08-05 18:33:11
【问题描述】:
我有一个应用程序需要将许多地址转换为 CLPlacemark 以获取纬度/经度。 在 Apple 文档中解释说我们不应该执行很多 GeoCode 请求,Geocoding 请求是有速率限制的。
在我的应用程序中,要执行地理编码,我只需请求一个地址,当我得到答案时,我会请求下一个地址,直到我收到所有地址的纬度/经度。
我会将这段代码放在一个专用线程中,以免阻塞用户。这似乎有效,但这是一个好策略吗?我如何确定 Apple 不会将我的应用程序列入黑名单或对我的请求返回错误。
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")
}
}
【问题讨论】: