执行许多地理编码请求的策略 [英] Strategy to perform many Geocode requests

查看:51
本文介绍了执行许多地理编码请求的策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要将许多地址转换为CLPlacemark以获得纬度/经度的应用程序. 在Apple文档中,解释了我们不应执行许多GeoCode请求,因为Geocoding请求受速率限制.

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.

我会将这段代码放在一个专用线程中,以免阻止用户.看来可行,但这是个好策略吗?如何确定Apple不会将我的应用列入黑名单或不会因我的请求而返回错误.

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

推荐答案

简短:也许在每个地理编码请求之前尝试sleep(2).

Short: Maybe just try sleep(2) before each geocoding request.

:虽然不能确定Apple是否会将其应用黑名单".我不确定是否存在很多用于地理编码的黑名单"应用.

Long: While one cannot be certain if Apple would 'blacklist' their app. I am not sure 'blacklisting' app for geocoding a lot exists.

我本人也尝试过与您类似的策略.仅使用外部变量queue并执行queue.popLast().这样做给了我100的结果,然后我开始对其余的地址报错.

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.

我在每个地理编码请求之前添加了sleep(2),并且我成功地提取了4000多个地理编码而没有任何阻塞.

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.

还请注意,我从未尝试过sleep(1),它在某种程度上也可以工作.

Also please note that I never tried sleep(1), which could also work to some extent.

根据Apple的 docs ,最大速率"为未指定,他们建议

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

我希望这会有所帮助.

干杯.

这篇关于执行许多地理编码请求的策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆