简体   繁体   English

Google Maps API v3提供了更多批量地理编码问题

[英]More batch geocoding questions with the Google Maps API v3

I'm trying to figure out a nice way to limit the rate at which I send geocode requests to the Google Maps API v3 geocoder service. 我正试图找出一种很好的方法来限制我向Google Maps API v3地理编码器服务发送地理编码请求的速率。 I know that Javascript does not have any nice wait or sleep because its execution is, for the time being, single-threaded. 我知道Javascript没有任何好的waitsleep因为它的执行暂时是单线程的。

Each geocoder request is sent inside of a jQuery each function. 每个地理编码器请求都是在jQuery的each函数内发送的。 So, the general code skeleton is: 所以,通用代码框架是:

$(xml).find('foo').each(function(){
    // do stuff
    ...

    geocoder.geocode(request, function(results, status) {/* do other stuff */});

    // do more stuff
    ...
}

How can I set a fixed interval to wait in between each call to geocode ? 如何设置固定间隔以在每次geocode调用之间等待? If I send each request as fast as Javascript will run, then I quickly start receiving OVER_QUERY_LIMIT responses - even if I'm only sending 20 requests. 如果我以Javascript运行的速度发送每个请求,那么我很快就会开始接收OVER_QUERY_LIMIT响应 - 即使我只发送了20个请求。 This is expected, and I'm trying to make my client play nicely with Google's service. 这是预期的,我正在尝试让我的客户端与Google的服务很好地协作。


An alternate route I'm willing to pursue is to completely abandon Javascript for geocoding, and write it all in Java. 我愿意采用的另一种方法是完全放弃Javascript进行地理编码,并用Java编写。 With Java it would be really easy to sleep in between requests. 使用Java,在请求之间睡眠非常容易。

However, I couldn't find a way to use Google's geocoding service (specifically, using version 3 of the API) in Java. 但是,我找不到在Java中使用Google的地理编码服务(特别是使用API​​的第3版)的方法。 GeoGoogle seems to be more than a year out of date, and uses v2. GeoGoogle似乎已经过时了一年多,并且使用了v2。

Can it be done in Java, and if so, how? 它可以用Java完成,如果是这样,怎么做?

Google is actively developing v2 and v3 of the Maps API separately. Google正在积极开发Maps API的v2和v3。 v3 is a much slimmer version suitable for devices with limited processing power or basic mapping tasks (and doesn't need an API key to work). v3是一个更纤薄的版本,适用于处理能力有限或基本映射任务的设备(并且不需要API密钥才能工作)。

In either case, if you're geocoding many things you should probably look at using Google's HTTP geocoding service ( http://code.google.com/apis/maps/documentation/geocoding/index.html ). 在任何一种情况下,如果您要对许多内容进行地理编码,您应该考虑使用Google的HTTP地理编码服务( http://code.google.com/apis/maps/documentation/geocoding/index.html )。 This will avoid the rate limiting issue. 这将避免速率限制问题。

And fwiw: Javascript can do basic sleep/wait operations using setInterval() and setTimeout(). 并且fwiw:Javascript可以使用setInterval()和setTimeout()进行基本的睡眠/等待操作。 Just fire the request and reset the timer to do it again at the end of your callback processing. 只需触发请求并重置计时器,以便在回调处理结束时再次执行此操作。

Your best bet to implement some sort of rate-limited submissions would be to use a timer object and a queue. 实现某种速率限制提交的最佳选择是使用计时器对象和队列。 The timer is scheduled at a fixed rate to run indefinitely (jQuery has some very nice timer implementations) and in the body of that timer, you pop something off the queue and submit it and then finish. 计时器按固定速率安排无限期运行(jQuery有一些非常好的计时器实现),并且在该计时器的主体中,你从队列中弹出一些东西然后提交它然后完成。 You other code then adds things to that queue as needed. 您可以根据需要将其他代码添加到该队列中。

Here is a snippet from my script, basically it tries again if it hits google.maps.GeocoderStatus.OVER_QUERY_LIMIT 这是我的脚本中的一个片段,基本上如果它点击google.maps.GeocoderStatus.OVER_QUERY_LIMIT它会再次尝试

function(results, status) {
    // If the limit is reached try again in a second
        if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {

            window.setTimeout(
                function() {self.resolveAddress(addr);},1000
            );
        } else if (status == google.maps.GeocoderStatus.OK) {
            // Do something clever
        } else if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {
            self.reduce(addr); // Try removing something from addr
        } else {
            atira.log('Something terrible happened');
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM