Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

javascript - Settimeout to avoid over_query-limit

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
        }
    });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...