通过jquery geocoder.geocode解析地址(400项) [英] Address geocode via jquery geocoder.geocode (400 Items)

查看:188
本文介绍了通过jquery geocoder.geocode解析地址(400项)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用400个地址解析一个json,并在每个位置上设置地图图标。我的问题是,当我循环的项目时,我得到一个错误:OVER_QUERY_LIMIT。但是,使用Google地理编码API设置职位的最佳方式是什么?
我的函数如下所示:

  function getAddresses(data){
var items,markers_data = [ ]。
if(data.addresses.length> 0){
items = data.addresses;

for(var i = 0; i< items.length; i ++){
var
item = items [i]
,street = item.address .street
,zip = item.address.zip
,city = item.address.city
,country = item.address.country;
var
geocoder = new google.maps.Geocoder()
,fulladdress = street +','+ zip +''+ city +','+ country;
$ b $ setTimeout(function(){
geocoder.geocode({'address':fulladdress},
function(results,status){
if(status == google.maps.GeocoderStatus.OK){

console.log(results [0] .geometry.location.lat());

console.log(results [0 ] .geometry.location.lng());
markers_data.push({
lat:results [0] .geometry.location.lat(),
lng:results [0]。 geometry.location.lng(),
title:item.name,
infoWindow:{
content:'< h2> + item.name +'< / h2>< p> ;'+ street + zip + city +'< / p>'
}
});
} else {
console.log('not geocoded');
console.log('status');
console.log(status);
}
});
},1000);
}
}

map.addMarkers(markers_data);
}

我尝试将geocoder.geocode函数放入超时函数,但不幸的是,不会帮助。我在我的js中使用了插件gmaps.js。

解决方案

每个会话配额都有一个geocoder服务,文档:


无论有多少用户共享同一个项目,每个用户会话都会应用速率限制。当你第一次加载API时,你被分配了一个初始的请求配额。一旦您使用此配额,API将按照每秒的额外请求强制执行速率限制。如果在特定时间段内发出的请求太多,那么API会返回一个OVER_QUERY_LIMIT响应代码。



每会话速率限制会阻止将客户端服务用于批量请求,例如批量地理编码。对于批量请求,请使用Google地图地理编码API网络服务。


https://developers.google.com/maps/documentation/javascript/geocoding#UsageLimits



因此,您应该限制您的客户端调用,并在最初的10个请求之后每秒执行1个请求,或者使用Geocoding API Web服务实现服务器端批处理地址解析,您可以每秒处理多达50个请求。 p>

顺便说一下,在您的代码中,您尝试在1秒后执行所有请求。你应该增加每个下一个请求的延迟。

  setTimeout(function(){
// Your code
},1000 * i);

所以第一个请求会立即执行,第二个1秒后,第二个2秒后,等等。


I am trying to parse an json with 400 addresses and set map icons on each location. My problem is, that when I am looping over the items I get an error: OVER_QUERY_LIMIT. But what is the best way to set the position with the google geocode api? My function looks like this:

        function getAddresses(data) {
            var items, markers_data = [];
            if (data.addresses.length > 0) {
                items = data.addresses;

                for (var i = 0; i < items.length; i++) {
                    var
                        item = items[i]
                         , street = item.address.street
                         , zip = item.address.zip
                         , city = item.address.city
                         , country = item.address.country;
                     var
                        geocoder =  new google.maps.Geocoder()
                         , fulladdress = street + ',' + zip + ' '+ city + ',' + country;

                    setTimeout(function () {
                         geocoder.geocode({'address': fulladdress}, 
                          function(results, status) {
                            if (status == google.maps.GeocoderStatus.OK) {

                               console.log(results[0].geometry.location.lat());

                               console.log(results[0].geometry.location.lng());     
                                markers_data.push({
                                     lat : results[0].geometry.location.lat(),
                                    lng : results[0].geometry.location.lng(),
                                     title: item.name,
                                     infoWindow: {
                                        content: '<h2>'+item.name+'</h2><p>'+ street + zip + city +'</p>'
                                     }
                                });
                            } else {
                                console.log('not geocoded');
                                console.log('status');
                                console.log(status);
                            }
                         });
                     }, 1000);
                }
             }

            map.addMarkers(markers_data);
        }

I tried to put my geocoder.geocode function in a timeout function but unfortunately this won´t help. I am using the plugin gmaps.js in my js.

解决方案

There is a per session quota for geocoder service as stated in the documentation:

The rate limit is applied per user session, regardless of how many users share the same project. When you first load the API, you are allocated an initial quota of requests. Once you use this quota, the API enforces rate limits on additional requests on a per-second basis. If too many requests are made within a certain time period, the API returns an OVER_QUERY_LIMIT response code.

The per-session rate limit prevents the use of client-side services for batch requests, such as batch geocoding. For batch requests, use the Google Maps Geocoding API web service.

https://developers.google.com/maps/documentation/javascript/geocoding#UsageLimits

So, you should throttle your client side calls and execute 1 request per second after initial 10 requests or implement a server side batch geocoding with Geocoding API web service where you can have up to 50 requests per second.

By the way in your code you try execute all requests after 1 second. You should increase a delay for each next request.

setTimeout(function () {
    //Your code
}, 1000 * i);

So the first request will be executed immediately, the second after 1 sec, the third after 2 seconds and so on.

这篇关于通过jquery geocoder.geocode解析地址(400项)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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