Settimeout to avoid over_query-limit

微笑、不失礼 提交于 2019-12-17 20:33:22

问题


i am trying to retrieve addresses using the googlemaps geocoder..but iam getting only few addresses ..as i see my javascript is failing to retrieve after 10 addresses..below is my code

function fetchData(lat,lng,type){   

    $('#placedata').empty();
    myLatlng = new google.maps.LatLng(parseFloat(lat), parseFloat(lng));
    map = new google.maps.Map(document.getElementById('map'), {
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                    center: myLatlng,
                    zoom: 10});
    var request = {location: myLatlng,radius: 50000,types: [type]};
    var service = new google.maps.places.PlacesService(map);
    service.search(request, callback);
  }

  function callback(results, status) {  

      for (var i = 0; i < results.length; i++) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {  
          createMarker(results[i]);     
        }       
    }
  }

  function createMarker(place) {
    place.geometry.location});

    var request = {reference: place.reference,};
    service = new google.maps.places.PlacesService(map);
    service.getDetails(request, detailsDisplay);

    function detailsDisplay(details, status) {      
        $('#placedata').append('<tr><td><a href='+details.url+'>'+details.name+'</a></td></tr>');

    }

}

As i see many have same problem ..is there a way or using setTimeout function and delay the request so i get get atleast 20 addresses..Any help would be appreciated..thankyou


回答1:


The solution is use setTimeout to prevent OVER_QUERY_LIMIT:

function createMarker(place) {
    //var placeLoc = place.geometry.location;
    //var marker = new google.maps.Marker({map: map,zIndex: 100,position: place.geometry.location});

    var request = {
        reference : place.reference,
    };

    service = new google.maps.places.PlacesService(map);

    service.getDetails(request, function(details, status) {
        if (status === google.maps.places.PlacesServiceStatus.OK) {
            $('#placedata').append('<tr><td><a href='+details.url+'>' + details.name + '</a></td></tr>');
        } else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
            setTimeout(function() {
                createMarker(place);
            }, 200);
        }
    });
}



回答2:


I have the solution for this.

try this

if(pagination.hasNextPage){
pagination.nextPage();
}

if(pagination.b == null){
createMarker(place);
}

//No need for setTimeout. try this. it will be helpful to you.



来源:https://stackoverflow.com/questions/12721287/settimeout-to-avoid-over-query-limit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!