超过查询限制

时间:2013-08-31 12:03:47

标签: javascript google-maps search limit google-places-api

这里我有一个代码,可以在框中搜索时向我显示谷歌放置对象。

所以:

function findPlaces(boxes,searchIndex) {
   var request = {
       bounds: boxes[searchIndex],
            types: ["museum"]
   };
   // alert(request.bounds);
   service.nearbySearch(request, function (results, status) {
   if (status != google.maps.places.PlacesServiceStatus.OK) {
     alert("Request["+searchIndex+"] failed: "+status);
     return;
   }
   // alert(results.length);
   document.getElementById('side_bar').innerHTML += "bounds["+searchIndex+"] returns "+results.length+" results<br>"
   for (var i = 0, result; result = results[i]; i++) {
     var marker = createMarker(result);
   }
   searchIndex++;
   if (searchIndex < boxes.length) 
     findPlaces(boxes,searchIndex);
   });
}

但是如果框为空,我会收到错误:Request[i].failed ZERO_RESULT 我的第一个问题是如何解决这个问题,那么如何转到下一个框,这只是因为没有结果而跳转

还有一段时间我得到OVER_QUERY_LIMIT - 我怎么能解决这个问题?

更新:我试着解决问题但是再次出现问题:

function findPlaces(boxes,searchIndex) {
   var request = {
       bounds: boxes[searchIndex],
            types: ["museum"]
   };
   // alert(request.bounds);
   service.nearbySearch(request, function (results, status) {
   if (status != google.maps.places.PlacesServiceStatus.OK) {
//JUMP TO THE NEXT
     searchIndex++;

   }
   // alert(results.length);
   document.getElementById('side_bar').innerHTML += "bounds["+searchIndex+"] returns "+results.length+" results<br>"
   for (var i = 0, result; result = results[i]; i++) {
     var marker = createMarker(result);
   }
//WAIT 3000 TO THE NEW REQUEST
      setTimeout(function () {
    alert('hello');
  }, 3000);
   searchIndex++;
   if (searchIndex < boxes.length) 
     findPlaces(boxes,searchIndex);
   });
}

1 个答案:

答案 0 :(得分:0)

service.nearbySearch(request, function (results, status) {
    if (status != google.maps.places.PlacesServiceStatus.OK) {
        // you probably don't even need the test for 'ZERO_RESULT' just test
        // your index against the boxes array for more items to search since
        // status could be a different error
        if((status == 'ZERO_RESULT') && (++searchIndex < boxes.length)){
            findPlaces(boxes,searchIndex);
        }else{
            return;
        }
    }else{
        document.getElementById('side_bar').innerHTML += "bounds["+searchIndex+"] returns "+results.length+" results<br>"
        for (var i = 0, result; result = results[i]; i++) {
            var marker = createMarker(result);
        }
        if (++searchIndex < boxes.length) 
            findPlaces(boxes,searchIndex);
    }
});

好的,我没有看到你想在侧边栏中输出结果,在这种情况下你不需要检查状态OK,因为结果无论如何都没有。试试这个:

service.nearbySearch(request, function (results, status) {
    document.getElementById('side_bar').innerHTML += "bounds["+searchIndex+"] returns "+results.length+" results<br>"
    for (var i = 0;i<results.length; i++) {
        var marker = createMarker(results[i]);
    }
    if (++searchIndex < boxes.length) 
        setTimeout(findPlaces(boxes,searchIndex),500);
});

编辑 - 也许这就是:

service.nearbySearch(request, function (results, status) {
    if(status == 'OVER_QUERY_LIMIT'){
        setTimeout(findPlaces(boxes,searchIndex),1000);
    }else{
        document.getElementById('side_bar').innerHTML += "bounds["+searchIndex+"] returns "+results.length+" results<br>"
        for (var i = 0;i<results.length; i++) {
            var marker = createMarker(results[i]);
        }
        if (++searchIndex < boxes.length) 
            setTimeout(findPlaces(boxes,searchIndex),1000);
    }
});