-1

I calculate the store location/distance of the current user location with Google Maps Directions API. It all works fine, but when I have more then 10 stores, the API stops running (limit on Google Maps Directions API).

I get the current user location with the HTML5 Geolocation:

get_user_location = function(){     
    if(navigator.geolocation){
        navigator.geolocation.getCurrentPosition(add_stores_to_array);
    }
};

And I calculate it with the Google Maps Directions Service:

var directionsService = new google.maps.DirectionsService();

var request = {
    origin: user,
    destination: store,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
};

directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
        var response = Math.ceil(response.routes[0].legs[0].distance.value / 1000);

How can I avoid the limit of the current Google Maps Directions Service?

Alter Ego
  • 51
  • 7
  • 2
    You can purchase premium access which allows up to 25 stops including origin and destination. Alternatively, you can use TSP algorithm which allows, theoretically, unlimited stops. I have personally tested it with over 100 stops, and does a decent job. – Adam Azad Jan 02 '17 at 12:11
  • But I can do 2500 requests a day? I have just 30 stores? – Alter Ego Jan 02 '17 at 12:50
  • What are you trying to do? Are you trying to find the closest stores to a location? Or a route to visit all the stores from that location? – geocodezip Jan 02 '17 at 13:43
  • I try to find the closest store. I loop through all stores, put them in an array and sort them based on the distance. – Alter Ego Jan 02 '17 at 13:44
  • BTW - the "limit of 10" you are running into is the rate limit on the directions service, if you look for "OVER_QUERY_LIMIT" status and handle that, you will get more results. – geocodezip Jan 02 '17 at 13:47

1 Answers1

1

The only option, if you want to use JavaScript API, and increase the 8 limit to purchasing premium plan. You can also use HTTP web service to get 25 waypoints (including origin and destination) for free. Notice the request must be accompanied by key and secret, and processed by server-side.

To use your own key, which is required by now as of June 22, 2016 updates to Google Maps API. To use a key, pass it as a query parameter

<script src="https://maps.googleapis.com/maps/api/js?key={API_KEY}"></script>

Regarding the premium key, if you have any concerns, the key can be protected by making it work only on whitelisted domains or HTTP referrers. These settings can be managed from in Key restriction section in Google Developers Console. Remember to always consider this--it is crucial.

key resctriction

Alternatively, there is another option within 8 limit domain using JavaScript API. Which is using travel salesman problem (TSP) algorithm -- Google Maps TSP Solver on GitHub. I have used this with 100 waypoints, and it does a decent job.

Adam Azad
  • 11,171
  • 5
  • 29
  • 70