user1608615
user1608615

Reputation: 3

Google Maps API v3 DirectionsService

I have had some problems with the directionsService object of google maps API v3.

The markersArray is an array that contains "google.Maps.Marker" objects and I want to calculate the distance between it. But the directionsService object sometimes returns "over_query_limit" error or "unknown_error", and I don't know why, because I consider the restriction time (I do a pause during 1 second for each 2 points)

But the most strange thing is that this problem only happens sometimes. Sometimes, the program runs correctly.

Can anyone propose me a solution?

function calcRoute()
{
    var start;
    var end;    

    for (var i = 0 ; i < markersArray.length - 1 ; i++)
    {
        start = markersArray[i].getPosition();

        for (var j = i+1 ; j < markersArray.length ; j++)
        {
            end = markersArray[j].getPosition();

            addRoute(start,end,i,j);
            addRoute(end,start,j,i);

            sleep(1000);
        }               
    }
}


function addRoute(start, end, i, j) 
{       
    var summaryPannel; 
    var directionsService = new google.maps.DirectionsService();    
    var request = 
    {
        origin: start, 
        destination: end,   
        travelMode: google.maps.DirectionsTravelMode.DRIVING,
        optimizeWaypoints: true
    };

    directionsService.route(request, function(response, status) 
    {       
        summaryPanel.innerHTML += status + "<br />";    

        if (status == google.maps.DirectionsStatus.OK) 
        {   

            summaryPanel.innerHTML += (i+1) + " " + (j+1) + "<br/>";            
        }
    });
}

Upvotes: 0

Views: 2392

Answers (1)

geocodezip
geocodezip

Reputation: 161404

The Google Directions service is subject to quotas and rate limits which are not fixed and will depend on the loading of the server. If you sometimes get over_query_limit results, then you need to either make your fixed delay longer or dynamically throttle the requests (retry the request with a longer delay when that happens).

If all you need is the distance, perhaps you could use the DistanceMatrix Service

Upvotes: 1

Related Questions