Google 使用地理编码器映射多请求

Google map multi request with geocoder

我的开发 google 地图需要一些帮助。事实上,当我尝试对一个地址进行地理编码时,google 毫无问题地向我提供了所有信息。

但是当我尝试对多个地址进行地理编码时,它结束了 google 没有回答,我也没有任何回应。

我的代码:

$.ajax({
type: "POST",
url: _URL_SITE + "webservices...",
dataType: "xml",
data: parameters,
//contentType: "application/soap+xml; charset=utf-8",
contentType: "text/xml; charset=utf-8",
headers: {
    Accept: '*',
    SOAPAction: 'http://127.0.0.1/...'
},
success: function (xml) {
    var arr = new Array(30000)
    var cpt = 1

    var address = [];
    var address_id = [];
    var codepostal = "";
    var i = 0;

    $(xml).find('Table').each(function () {
        codepostal = $(this).find('cp').text(); 
        address[i] = $(this).find('record_id').text() + '|' + codepostal.substring(0,3) + '* ' + $(this).find('commune').text() + '' + '';
        i++;
    }); // fin de la fonction jquery

    traitement(address);      
},
error: function () {
    alert('error');
}

});

这里是请求 google 的函数:

function traitement(addresses, callback) {
var coords = [];
var ville = "";
var cp = "";

var lattitude;
var longitude;


    for (var i = 0; i < 50; i++) {
        currAddress = addresses[i].split("|");
        var geocoder = new google.maps.Geocoder();

        geocoder.geocode({ 'address': currAddress[1] }, function (results, status) {
            if (status == 'OK') {
                var coords = results[0].geometry.location
                lattitude = coords.lat();
                longitude = coords.lng();

                var latlng = new google.maps.LatLng(lattitude, longitude);
                geocoder.geocode({ 'latLng': latlng }, function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        var elt = results[0].address_components;
                        for (i in elt) {
                            if (elt[i].types[0] == 'postal_code')
                                cp = elt[i].short_name;
                            if (elt[i].types[0] == 'sublocality')
                                ville = elt[i].short_name;
                            if (ville == "") {
                                if (elt[i].types[0] == 'locality')
                                    ville = elt[i].short_name;
                            }

                        }
                        // function which save google's information
                        Save(currAddress[0], cp, ville, longitude, lattitude)
                        return;

                    } else { alert("error lat long"); }
                });

            } else { alert("error adresse"); }
            return;
        });
}

我已经阅读了一些关于异步请求等的帖子。但是我试图理解为什么当我的循环 "for" 的迭代次数超过 50 次时 google 给我 0 个答案。

谢谢你。

您正在使用地图 JavaScript API 的地理编码服务。 JavaScript API 中的服务也有每次会话限制。文档说:

Service requests are rate-limited per user session, regardless of how many users share the same project. When you first load the service 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. For batch requests, use the Maps API web services.

https://developers.google.com/maps/documentation/javascript/usage

最初你有一个包含 10 个查询的桶。使用这些查询后,您必须每秒发送 1 个查询以保持在允许的用户会话限制内。