【问题标题】:Address geocode via jquery geocoder.geocode (400 Items)通过 jquery geocoder.geocode 地址地理编码(400 项)
【发布时间】:2017-10-06 01:11:54
【问题描述】:

我正在尝试解析具有 400 个地址的 json,并在每个位置设置地图图标。我的问题是,当我遍历这些项目时,我得到一个错误:OVER_QUERY_LIMIT。但是使用 google geocode 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;

                    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。

【问题讨论】:

    标签: jquery json google-maps-api-3 gmaps.js


    【解决方案1】:

    如文档中所述,地理编码器服务的每个会话配额:

    速率限制适用于每个用户会话,无论有多少用户共享同一个项目。首次加载 API 时,会为您分配初始请求配额。使用此配额后,API 会按每秒对其他请求实施速率限制。如果在特定时间段内发出过多请求,API 会返回 OVER_QUERY_LIMIT 响应码。

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

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

    因此,您应该限制客户端调用并在初始 10 个请求后每秒执行 1 个请求,或者使用 Geocoding API Web 服务实施服务器端批量地理编码,每秒最多可以处理 50 个请求。

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

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

    所以第一个请求将立即执行,第二个在 1 秒后执行,第三个在 2 秒后执行,依此类推。

    【讨论】:

      猜你喜欢
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      • 2021-05-26
      相关资源
      最近更新 更多