0

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

所以:

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

1 回答 1

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);
    }
});
于 2013-08-31T12:58:39.153 回答