Google Map Direction request failed due to OVER_QUERY_LIMIT - Pause JavaScript function or PHP code

1.9k views Asked by At

I am getting OVER_QUERY_LIMIT when sending several request for direction service. I know that it is a well-known issue and there are several solutions to delay the request sent but I am not able to do it beacause my request are sent in a php for loop.Please help !!!

Here is part of my php code (the code is working fine but at some point in time I get the OVER_QUERY_LIMIT error:

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

 echo"script>

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

</script> "

}

Here is the function :

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

}

How can I pause the php code or the javascript function ?

1

There are 1 answers

2
AudioBubble On

I can't test your source , but it seems this is a solution wait for 1 second or more , try it ...

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

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

                ...
            }