OVER_QUERY_LIMIT in Google Maps API v3: How do I pause/delay in Javascript to slow it down?

前端 未结 6 2025
暖寄归人
暖寄归人 2020-11-22 08:49

I\'m hitting an issue that is WELL discussed in these forums, but none of the recommendations seem to be working for me so I\'m looking for some full javascript that works w

6条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 09:12

    Here I have loaded 2200 markers. It takes around 1 min to add 2200 locations. https://jsfiddle.net/suchg/qm1pqunz/11/

    //function to get random element from an array
        (function($) {
            $.rand = function(arg) {
                if ($.isArray(arg)) {
                    return arg[$.rand(arg.length)];
                } else if (typeof arg === "number") {
                    return Math.floor(Math.random() * arg);
                } else {
                    return 4;  // chosen by fair dice roll
                }
            };
        })(jQuery);
    
    //start code on document ready
    $(document).ready(function () {
        var map;
        var elevator;
        var myOptions = {
            zoom: 0,
            center: new google.maps.LatLng(35.392738, -100.019531), 
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map($('#map_canvas')[0], myOptions);
    
        //get place from inputfile.js
        var placesObject = place;
        errorArray = [];
    
      //will fire 20 ajax request at a time and other will keep in queue
        var queuCounter = 0, setLimit = 20; 
    
      //keep count of added markers and update at top
      totalAddedMarkers = 0;
    
      //make an array of geocode keys to avoid the overlimit error
        var geoCodKeys = [
                        'AIzaSyCF82XXUtT0vzMTcEPpTXvKQPr1keMNr_4',
                        'AIzaSyAYPw6oFHktAMhQqp34PptnkDEdmXwC3s0',
                        'AIzaSyAwd0OLvubYtKkEWwMe4Fe0DQpauX0pzlk',
                        'AIzaSyDF3F09RkYcibDuTFaINrWFBOG7ilCsVL0',
                        'AIzaSyC1dyD2kzPmZPmM4-oGYnIH_0x--0hVSY8'                   
                    ];
    
      //funciton to add marker
        var addMarkers = function(address, queKey){
            var key = jQuery.rand(geoCodKeys);
            var url = 'https://maps.googleapis.com/maps/api/geocode/json?key='+key+'&address='+address+'&sensor=false';
    
            var qyName = '';
            if( queKey ) {
                qyName = queKey;
            } else {
                qyName = 'MyQueue'+queuCounter;
            }
    
            $.ajaxq (qyName, {
                url: url,
                dataType: 'json'
            }).done(function( data ) {
                        var address = getParameterByName('address', this.url);
                        var index = errorArray.indexOf(address);
                        try{
                            var p = data.results[0].geometry.location;
                            var latlng = new google.maps.LatLng(p.lat, p.lng);
                            new google.maps.Marker({
                                position: latlng,
                                map: map
                            });
                            totalAddedMarkers ++;
    
                //update adde marker count
                            $("#totalAddedMarker").text(totalAddedMarkers);
                            if (index > -1) {
                                errorArray.splice(index, 1);
                            }
                        }catch(e){
                            if(data.status = 'ZERO_RESULTS')
                                return false;
    
                //on error call add marker function for same address
                //and keep in Error ajax queue
                            addMarkers( address, 'Errror' );
                            if (index == -1) {
                                errorArray.push( address );
                            }
                        }
            });
    
        //mentain ajax queue set
            queuCounter++;
            if( queuCounter == setLimit ){
                queuCounter = 0;
            }
        }
    
      //function get url parameter from url string
        getParameterByName = function ( name,href )
        {
          name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
          var regexS = "[\\?&]"+name+"=([^&#]*)";
          var regex = new RegExp( regexS );
          var results = regex.exec( href );
          if( results == null )
            return "";
          else
            return decodeURIComponent(results[1].replace(/\+/g, " "));
        }
    
      //call add marker function for each address mention in inputfile.js
        for (var x = 0; x < placesObject.length; x++) {
            var address = placesObject[x]['City'] + ', ' + placesObject[x]['State'];
            addMarkers(address);
        }
    });
    

提交回复
热议问题