2016-12-17 34 views
0

發送多個方向服務請求時,我得到OVER_QUERY_LIMIT。我知道這是一個衆所周知的問題,並有幾種解決方案來延遲發送的請求,但我無法做到這一點,因爲我的請求是在一個PHP for循環發送的。請幫助!由於OVER_QUERY_LIMIT導致Google Map方向請求失敗 - 暫停JavaScript函數或PHP代碼

這裏是我的PHP代碼的一部分(代碼工作正常,但在某個時間點,我得到了OVER_QUERY_LIMIT錯誤:

for($i=0;$i<count($PlanningArray);$i++){ 

echo"script> 

initMap('$Userlocation','$ActLocation1','$ActLocation2','$DivMapID' 

</script> " 

} 

下面是函數:

function initMap(Userloc,Actloc1,Actloc2,i) { 


    var renderOptions = {draggable:true}; 

    var directionsDisplay = new google.maps.DirectionsRenderer(renderOptions); 
    var directionsService = new google.maps.DirectionsService; 

    var mapOptions = { 
     zoom: 11, 
     center: {lat:-20.239340, lng:57.574604} 
    }; 

    var map = new google.maps.Map(document.getElementById('map' + i), mapOptions); 

    directionsDisplay.setMap(map); 

    var items = [Actloc1,Actloc2]; 

    var destination; 

    if(Actloc2 == ''){ 

     destination = Actloc1; 
    } 

    else { 

     destination = Actloc2; 
    } 

    var waypoints = []; 

    for (var j = 0; j < items.length; j++) { 

     var address = items[j]; 

     if (address !== '') { 
      waypoints.push({ 
       location: address, 
       stopover: true 
      }); 
     } 
    } 

    directionsService.route({ 

     origin: Userloc, 
     destination: destination, 
     travelMode: 'DRIVING', 
     waypoints: waypoints 

    }, function(response, status) { 
     if (status === 'OK') { 
      directionsDisplay.setDirections(response); 
     } else { 
      window.alert('Directions request failed due to ' + status); 
     } 
    }); 

} 

哪有我暫停php代碼或javascript函數?

回答

0

我無法測試您的源代碼,但似乎這是一個解決方案 等待1秒或更長時間,嘗試一下...

 if (stripos($ret,"OVER_QUERY_LIMIT")) 
      { 
       echo "<font color=red>OVER_QUERY_LIMIT waiting for 1 sec</font>"; 

       ob_flush(); 
       flush(); 
       sleep(1); 

       ... 
      } 
+0

$ ret是爲了什麼? –

+0

$ ret是查詢返回的值,因此您可以驗證是否等於「OVER_QUERY_LIMIT」 – 2016-12-17 13:31:27

相關問題