3

I am using google.maps.DirectionsService for getting the route between two points. This code was working from past 8 months. However, from past couple of days DirectionService route call is returning OVER_QUERY_LIMIT response status. There are only 6 set of points, out of which only 2 or 3 requests are getting the result, rest are failing. Code is unchanged from past 8 months. Below is the code snippet for reference:

            var directionsService = new google.maps.DirectionsService();
            var request = {                     
                    origin:originLatlng, //This is of type : google.maps.LatLng
                    destination:destLatlng,
                    travelMode: google.maps.DirectionsTravelMode.DRIVING,
                    provideRouteAlternatives: true
            };

            directionsService.route(request, function(result, status) {
                if (status == google.maps.DirectionsStatus.OK) {

                    polyline = new google.maps.Polyline({
                        path: result.routes[0].overview_path,
                        strokeColor: color1,
                        strokeOpacity: 0.8,
                        strokeWeight: 5,
                        geodesic: true
                    });
                }
            }

Almost 6 such simultaneous requests are made to DirectionService. I cannot put sleep in between the requests because, it will increase my application GUI load time.

I have tried the same code from different networks also, still problem persists.

I definitely did not come anywhere close to reaching the 2,500 daily request limit.

What could be the problem here? Please suggest a solution for this.

Any help would be greatly appreciated.

Thanks in advance

Satyapal

user1469358
  • 31
  • 1
  • 2
  • Might want to start by searching Stack Overflow and seeing the responses that folks have had to pretty much the same question: http://stackoverflow.com/search?q=over_query_limit – andresf Jun 20 '12 at 19:47
  • I am not able to solve this problem still. Does anyone have an answer for this? Please help. – user1469358 Jun 27 '12 at 05:33
  • I get over_query_limit after 10 paths gets processed. Can somebody tell solution to it? – Haseeb Jadoon Dec 06 '14 at 11:30

3 Answers3

1

There are 2 kind of quota for Google Map API. You are using client side quota which blocks you if you request more than ~20 ( thats my observation ) geocode per minute.

Look here for detailed information :

https://developers.google.com/maps/articles/geocodestrat#client

Luffy
  • 2,257
  • 16
  • 24
0

Try putting your request inside a setTimeout() function: the query_limit could be referred to past requests too near from this one.

setTimeout(function(){
    directionsService.route(request,function(result, status) {
        if (status == google.maps.DirectionsStatus.OK)
            {[...your code...]}
        else alert(status);
    });
},1000);
Matteo-SoftNet
  • 531
  • 3
  • 10
0

I followed Google's API for server-side directions and created a PHP page which will sleep(0.2) if an OVER_QUERY_LIMIT is returned as the status. I then used $.getJSON to retrieve this PHP page, which uses almost the exact same data and parameters. (Follow Google's directions to deal with the differences in the parameters.)

PHP:

$params = http_build_query($_GET);
$url = "http://maps.googleapis.com/maps/api/directions/json?sensor=false&" . $params;
$json = file_get_contents($url);
$status = json_decode($json)->status;

// check for over_query_limit status
while ($status=="OVER_QUERY_LIMIT") {
    sleep(0.2); // seconds
    $json = file_get_contents($url);
    $status = json_decode($json)->status;
}

header('application/json');
echo $json;

jQuery:

request = { // construct route request object
    origin: start_address,
    destination: end_address,
    waypoints: addresses.join('|'),
    avoid: 'tolls'
};

$.getJSON('directions-lookup.php', request, function(response) {
    //console.log(response);
    var status = response.status;
    if (status==google.maps.DirectionsStatus.OK) {
        // do things here
    };
}); 

Something I noticed is that when you're using the JavaScript Directions API, you'll get back things like lat/lng locations as Google LatLng objects, which doesn't happen here. I just used new google.maps.LatLng(..., ...) to build those back into objects.

Blazemonger
  • 90,923
  • 26
  • 142
  • 180