Settimeout以避免over_query-limit

时间:2012-10-04 06:12:06

标签: javascript google-maps-api-3 google-geocoder

我正在尝试使用googlemaps地理编码器检索地址..但我只获得了几个地址..我看到我的javascript无法在10个地址后检索..我是我的代码

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

    }

}

因为我看到很多人有同样的问题..有一种方法或使用setTimeout函数并延迟请求所以我得到至少20个地址..任何帮助将不胜感激..thankyou

2 个答案:

答案 0 :(得分:4)

解决方案是使用setTimeout来阻止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);
        }
    });
}

答案 1 :(得分:-1)

我有解决方案。

试试这个

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

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

//不需要setTimeout。试试这个。它会对你有所帮助。