Google map geocoding stops, while executing recurs

2019-07-25 09:26发布

i am using following function to load latlng of addresses From an array to array about 40, but it stops after 10 iterations.

 loadStoreLatLong(40);


   function loadStoreLatLong(i){     


    var paddress = store_locations[i]['address'] +','+store_locations[i]['postal_code'] + ', ' +store_locations[i]['district'] + ',' + store_locations[i]['country'];
    var  pgeocoder = new google.maps.Geocoder();

    pgeocoder.geocode( { 'address': paddress}, function(results, status) {  
        if (status == google.maps.GeocoderStatus.OK) {

        Storelatlong[i] = results[0].geometry.location;
        i--;

        if(i<0){

   clientLocation();

        }else{

   loadStoreLatLong(i);

    //var t = setTimeout("loadStoreLatLong("+(i)+")",1000);
    //jQuery('#map_canvas').text('Loading ... '+i);

        }
        }
    });
}

i am really stuck there, can any one help to figure it out, when i set time out 1000, this iterates fine 40 time, when i decrease the timeout, iterations also decreases and stop. thanks

2条回答
Fickle 薄情
2楼-- · 2019-07-25 10:08

Geocoder of Google Maps API Javascript is not suite for much points. They denies continuous accesses in short time.

So please use REST version even if you have 40 points. https://developers.google.com/maps/documentation/geocoding/

查看更多
劳资没心,怎么记你
3楼-- · 2019-07-25 10:18

The client geocoder is not intended for geocoding lots of points on page load. It is intended for geocoding user entered addresses.

It is asynchronous and is subject to a quota and rate limit. The limits are set up such that you can geocode ~10 points quickly (depending on server load).

You should check the status returned, if it indicates that you are over the limit, delay for a period of time and retry.

See Andrew's answer to this question

查看更多
登录 后发表回答