Exceed 23 waypoint per request limit on Google Directions API (Business/Work level)












24














I would like to use the google directions API to develop route planning software for a company that handles snowplows in the winter and landscaping in the summer. One of the customers requirements is that he be able to calculate routes with at least 30 ( preferably more ) waypoints. According to the documentation (quoted below) even Google Maps API for Work customers are limited to just 23 waypoints per request.




Use of the Google Directions API is subject to a query limit of 2,500 directions requests per day. Individual directions requests may contain up to 8 intermediate waypoints in the request. Google Maps API for Work customers may query up to 100,000 directions requests per day, with up to 23 waypoints allowed in each request.




Is anyone aware of a workaround -- any way at all -- to get around this?



Also -- might it be possible to use a workaround for the free API? I hear the premier accounts are quite expensive.



Thanks!!
Marc










share|improve this question




















  • 2




    Here's my solution to the problem. Notes: - Set MAX_WAYPOINTS_PER_REQUEST to 8, 23, or whatever you like. - Each request object that is sent to Google is also logged in browser console for you to inspect.
    – CrazyTim
    Jul 5 '12 at 1:54












  • hey @CrazyTim i am getting status="ZERO_RESULTS" for some point. if i remove the point its working. how to skip the point
    – Nullify
    Sep 30 '13 at 13:56










  • @Kousik, I don't know of a way to isolate the latlong that is causing the ZERO_RESULTS error other than manually checking each link in the chain. It might be best posting this as a separate question.
    – CrazyTim
    Oct 1 '13 at 1:49










  • hey i put one question in stack i think you will be the best person for the ans. thanks stackoverflow.com/questions/19116578/…
    – Nullify
    Oct 1 '13 at 12:47












  • @CrazyTim i fixed the problem see this jsfiddle.net/67tbw/29
    – Nullify
    Oct 3 '13 at 11:26
















24














I would like to use the google directions API to develop route planning software for a company that handles snowplows in the winter and landscaping in the summer. One of the customers requirements is that he be able to calculate routes with at least 30 ( preferably more ) waypoints. According to the documentation (quoted below) even Google Maps API for Work customers are limited to just 23 waypoints per request.




Use of the Google Directions API is subject to a query limit of 2,500 directions requests per day. Individual directions requests may contain up to 8 intermediate waypoints in the request. Google Maps API for Work customers may query up to 100,000 directions requests per day, with up to 23 waypoints allowed in each request.




Is anyone aware of a workaround -- any way at all -- to get around this?



Also -- might it be possible to use a workaround for the free API? I hear the premier accounts are quite expensive.



Thanks!!
Marc










share|improve this question




















  • 2




    Here's my solution to the problem. Notes: - Set MAX_WAYPOINTS_PER_REQUEST to 8, 23, or whatever you like. - Each request object that is sent to Google is also logged in browser console for you to inspect.
    – CrazyTim
    Jul 5 '12 at 1:54












  • hey @CrazyTim i am getting status="ZERO_RESULTS" for some point. if i remove the point its working. how to skip the point
    – Nullify
    Sep 30 '13 at 13:56










  • @Kousik, I don't know of a way to isolate the latlong that is causing the ZERO_RESULTS error other than manually checking each link in the chain. It might be best posting this as a separate question.
    – CrazyTim
    Oct 1 '13 at 1:49










  • hey i put one question in stack i think you will be the best person for the ans. thanks stackoverflow.com/questions/19116578/…
    – Nullify
    Oct 1 '13 at 12:47












  • @CrazyTim i fixed the problem see this jsfiddle.net/67tbw/29
    – Nullify
    Oct 3 '13 at 11:26














24












24








24


11





I would like to use the google directions API to develop route planning software for a company that handles snowplows in the winter and landscaping in the summer. One of the customers requirements is that he be able to calculate routes with at least 30 ( preferably more ) waypoints. According to the documentation (quoted below) even Google Maps API for Work customers are limited to just 23 waypoints per request.




Use of the Google Directions API is subject to a query limit of 2,500 directions requests per day. Individual directions requests may contain up to 8 intermediate waypoints in the request. Google Maps API for Work customers may query up to 100,000 directions requests per day, with up to 23 waypoints allowed in each request.




Is anyone aware of a workaround -- any way at all -- to get around this?



Also -- might it be possible to use a workaround for the free API? I hear the premier accounts are quite expensive.



Thanks!!
Marc










share|improve this question















I would like to use the google directions API to develop route planning software for a company that handles snowplows in the winter and landscaping in the summer. One of the customers requirements is that he be able to calculate routes with at least 30 ( preferably more ) waypoints. According to the documentation (quoted below) even Google Maps API for Work customers are limited to just 23 waypoints per request.




Use of the Google Directions API is subject to a query limit of 2,500 directions requests per day. Individual directions requests may contain up to 8 intermediate waypoints in the request. Google Maps API for Work customers may query up to 100,000 directions requests per day, with up to 23 waypoints allowed in each request.




Is anyone aware of a workaround -- any way at all -- to get around this?



Also -- might it be possible to use a workaround for the free API? I hear the premier accounts are quite expensive.



Thanks!!
Marc







google-maps google-maps-api-3 google-direction






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 6 '15 at 9:28









CodeBrauer

1,45311131




1,45311131










asked Jan 8 '12 at 18:04









Marc H

58821228




58821228








  • 2




    Here's my solution to the problem. Notes: - Set MAX_WAYPOINTS_PER_REQUEST to 8, 23, or whatever you like. - Each request object that is sent to Google is also logged in browser console for you to inspect.
    – CrazyTim
    Jul 5 '12 at 1:54












  • hey @CrazyTim i am getting status="ZERO_RESULTS" for some point. if i remove the point its working. how to skip the point
    – Nullify
    Sep 30 '13 at 13:56










  • @Kousik, I don't know of a way to isolate the latlong that is causing the ZERO_RESULTS error other than manually checking each link in the chain. It might be best posting this as a separate question.
    – CrazyTim
    Oct 1 '13 at 1:49










  • hey i put one question in stack i think you will be the best person for the ans. thanks stackoverflow.com/questions/19116578/…
    – Nullify
    Oct 1 '13 at 12:47












  • @CrazyTim i fixed the problem see this jsfiddle.net/67tbw/29
    – Nullify
    Oct 3 '13 at 11:26














  • 2




    Here's my solution to the problem. Notes: - Set MAX_WAYPOINTS_PER_REQUEST to 8, 23, or whatever you like. - Each request object that is sent to Google is also logged in browser console for you to inspect.
    – CrazyTim
    Jul 5 '12 at 1:54












  • hey @CrazyTim i am getting status="ZERO_RESULTS" for some point. if i remove the point its working. how to skip the point
    – Nullify
    Sep 30 '13 at 13:56










  • @Kousik, I don't know of a way to isolate the latlong that is causing the ZERO_RESULTS error other than manually checking each link in the chain. It might be best posting this as a separate question.
    – CrazyTim
    Oct 1 '13 at 1:49










  • hey i put one question in stack i think you will be the best person for the ans. thanks stackoverflow.com/questions/19116578/…
    – Nullify
    Oct 1 '13 at 12:47












  • @CrazyTim i fixed the problem see this jsfiddle.net/67tbw/29
    – Nullify
    Oct 3 '13 at 11:26








2




2




Here's my solution to the problem. Notes: - Set MAX_WAYPOINTS_PER_REQUEST to 8, 23, or whatever you like. - Each request object that is sent to Google is also logged in browser console for you to inspect.
– CrazyTim
Jul 5 '12 at 1:54






Here's my solution to the problem. Notes: - Set MAX_WAYPOINTS_PER_REQUEST to 8, 23, or whatever you like. - Each request object that is sent to Google is also logged in browser console for you to inspect.
– CrazyTim
Jul 5 '12 at 1:54














hey @CrazyTim i am getting status="ZERO_RESULTS" for some point. if i remove the point its working. how to skip the point
– Nullify
Sep 30 '13 at 13:56




hey @CrazyTim i am getting status="ZERO_RESULTS" for some point. if i remove the point its working. how to skip the point
– Nullify
Sep 30 '13 at 13:56












@Kousik, I don't know of a way to isolate the latlong that is causing the ZERO_RESULTS error other than manually checking each link in the chain. It might be best posting this as a separate question.
– CrazyTim
Oct 1 '13 at 1:49




@Kousik, I don't know of a way to isolate the latlong that is causing the ZERO_RESULTS error other than manually checking each link in the chain. It might be best posting this as a separate question.
– CrazyTim
Oct 1 '13 at 1:49












hey i put one question in stack i think you will be the best person for the ans. thanks stackoverflow.com/questions/19116578/…
– Nullify
Oct 1 '13 at 12:47






hey i put one question in stack i think you will be the best person for the ans. thanks stackoverflow.com/questions/19116578/…
– Nullify
Oct 1 '13 at 12:47














@CrazyTim i fixed the problem see this jsfiddle.net/67tbw/29
– Nullify
Oct 3 '13 at 11:26




@CrazyTim i fixed the problem see this jsfiddle.net/67tbw/29
– Nullify
Oct 3 '13 at 11:26












5 Answers
5






active

oldest

votes


















15














You are correct the premier pricing is rather expensive starting at $10,000, last time I spoke with a google rep on the phone.



I found a workaround that I put into place to in a way bypasss the 8 waypoints limitation. I was able to make it work.



I did this by receiving my waypoints and breaking them up into different routes but drawing them together as the same route.



Example being if there were 30 waypoints needed I would draw 4 lines, but with the same color etc. So, basically you cut the waypoints into different routes calling the directions renderer each time as if it was a different route. The key is after the first route that the next route has to start with the last waypoint of the previous route (this makes sure that the route lines are connected to each other)



It works but you need to write a lot more code than what you would if you had a premier account, and you are calling for directions a lot more in this instance.



I have searched and thought about other ways to do this without having a premier account and have failed.



Although, I when speaking with google they did say that they intended on creating a tiered structure of payment for customers with different wants/needs. For instance, if a customer just needed more waypts and not a bunch more direction requests.



Hope this helps, as it worked for me in a practice application.






share|improve this answer

















  • 20




    Google really think everyone is as rich as them
    – Pacerier
    Jan 10 '12 at 19:45






  • 1




    This works for me and I know of several others that use it this way. It only works if you aren't hitting it thousands of times a day.
    – Bill Blankenship
    Jul 2 '12 at 13:46










  • What is working for you? Where is the hack? In the direction api a direction has 23 waypoints but what is the hack? It's like in my answer is about the loading of the library not how many waypoints of direction you fire on Google????
    – Bytemain
    Jul 2 '12 at 14:23










  • Please read this link. We are not talking about tsp problem. We are talking about waypoint limits via the directions renderer and how to plot a route that has more than the maximum allowed via this usage limit that google provides. developers.google.com/maps/documentation/business/…
    – Bill Blankenship
    Jul 2 '12 at 14:55












  • If you don't understand this please contact google their is a phone number on there.
    – Bill Blankenship
    Jul 2 '12 at 14:57



















10

















function initMap() {
var service = new google.maps.DirectionsService;
var map = new google.maps.Map(document.getElementById('map'));

// list of points
var stations = [
{lat: 48.9812840, lng: 21.2171920, name: 'Station 1'},
{lat: 48.9832841, lng: 21.2176398, name: 'Station 2'},
{lat: 48.9856443, lng: 21.2209088, name: 'Station 3'},
{lat: 48.9861461, lng: 21.2261563, name: 'Station 4'},
{lat: 48.9874682, lng: 21.2294855, name: 'Station 5'},
{lat: 48.9909244, lng: 21.2295512, name: 'Station 6'},
{lat: 48.9928871, lng: 21.2292352, name: 'Station 7'},
{lat: 48.9921334, lng: 21.2246742, name: 'Station 8'},
{lat: 48.9943196, lng: 21.2234792, name: 'Station 9'},
{lat: 48.9966345, lng: 21.2221262, name: 'Station 10'},
{lat: 48.9981191, lng: 21.2271386, name: 'Station 11'},
{lat: 49.0009168, lng: 21.2359527, name: 'Station 12'},
{lat: 49.0017950, lng: 21.2392890, name: 'Station 13'},
{lat: 48.9991912, lng: 21.2398272, name: 'Station 14'},
{lat: 48.9959850, lng: 21.2418410, name: 'Station 15'},
{lat: 48.9931772, lng: 21.2453901, name: 'Station 16'},
{lat: 48.9963512, lng: 21.2525850, name: 'Station 17'},
{lat: 48.9985134, lng: 21.2508423, name: 'Station 18'},
{lat: 49.0085000, lng: 21.2508000, name: 'Station 19'},
{lat: 49.0093000, lng: 21.2528000, name: 'Station 20'},
{lat: 49.0103000, lng: 21.2560000, name: 'Station 21'},
{lat: 49.0112000, lng: 21.2590000, name: 'Station 22'},
{lat: 49.0124000, lng: 21.2620000, name: 'Station 23'},
{lat: 49.0135000, lng: 21.2650000, name: 'Station 24'},
{lat: 49.0149000, lng: 21.2680000, name: 'Station 25'},
{lat: 49.0171000, lng: 21.2710000, name: 'Station 26'},
{lat: 49.0198000, lng: 21.2740000, name: 'Station 27'},
{lat: 49.0305000, lng: 21.3000000, name: 'Station 28'},
];

// Zoom and center map automatically by stations (each station will be in visible map area)
var lngs = stations.map(function(station) { return station.lng; });
var lats = stations.map(function(station) { return station.lat; });
map.fitBounds({
west: Math.min.apply(null, lngs),
east: Math.max.apply(null, lngs),
north: Math.min.apply(null, lats),
south: Math.max.apply(null, lats),
});

// Show stations on the map as markers
for (var i = 0; i < stations.length; i++) {
if (!stations[i].name)
continue;
new google.maps.Marker({
position: stations[i],
map: map,
title: stations[i].name
});
}

// Divide route to several parts because max stations limit is 25 (23 waypoints + 1 origin + 1 destination)
for (var i = 0, parts = , max = 8 - 1; i < stations.length; i = i + max)
parts.push(stations.slice(i, i + max + 1));

// Callback function to process service results
var service_callback = function(response, status) {
if (status != 'OK') {
console.log('Directions request failed due to ' + status);
return;
}
var renderer = new google.maps.DirectionsRenderer;
renderer.setMap(map);
renderer.setOptions({ suppressMarkers: true, preserveViewport: true });
renderer.setDirections(response);
};

// Send requests to service to get route (for stations count <= 25 only one request will be sent)
for (var i = 0; i < parts.length; i++) {
// Waypoints does not include first station (origin) and last station (destination)
var waypoints = ;
for (var j = 1; j < parts[i].length - 1; j++)
waypoints.push({location: parts[i][j], stopover: false});
// Service options
var service_options = {
origin: parts[i][0],
destination: parts[i][parts[i].length - 1],
waypoints: waypoints,
travelMode: 'WALKING'
};
// Send request
service.route(service_options, service_callback);
}
}

html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
width: 100%;
height: 100%;
}

<div id="map"></div>

<!-- without API KEY set variable "max" to 8 -->
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script>

<!-- with API KEY set variable "max" to 25 -->
<!-- <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap&key=YOUR_API_KEY"></script>-->





With following code you can use as many waypoints as you need and you will never get error MAX_WAYPOINTS_EXCEEDED. Do not forget to replace "YOUR_API_KEY" to your API KEY or remove &key=YOUR_API_KEY from google API URL and set variable "max" to 8 (max = 25 when using API KEY, max = 8 when not using API KEY).





<style>
html, body { height: 100%; margin: 0; padding: 0; }
#map { height: 100%; width: 100%; height: 100%; }
</style>
<div id="map"></div>
<script>
function initMap() {
var service = new google.maps.DirectionsService;
var map = new google.maps.Map(document.getElementById('map'));

// list of points
var stations = [
{lat: 48.9812840, lng: 21.2171920, name: 'Station 1'},
{lat: 48.9832841, lng: 21.2176398, name: 'Station 2'},
{lat: 48.9856443, lng: 21.2209088, name: 'Station 3'},
{lat: 48.9861461, lng: 21.2261563, name: 'Station 4'},
{lat: 48.9874682, lng: 21.2294855, name: 'Station 5'},
{lat: 48.9909244, lng: 21.2295512, name: 'Station 6'},
{lat: 48.9928871, lng: 21.2292352, name: 'Station 7'},
{lat: 48.9921334, lng: 21.2246742, name: 'Station 8'},
{lat: 48.9943196, lng: 21.2234792, name: 'Station 9'},
{lat: 48.9966345, lng: 21.2221262, name: 'Station 10'},
{lat: 48.9981191, lng: 21.2271386, name: 'Station 11'},
{lat: 49.0009168, lng: 21.2359527, name: 'Station 12'},
{lat: 49.0017950, lng: 21.2392890, name: 'Station 13'},
{lat: 48.9991912, lng: 21.2398272, name: 'Station 14'},
{lat: 48.9959850, lng: 21.2418410, name: 'Station 15'},
{lat: 48.9931772, lng: 21.2453901, name: 'Station 16'},
{lat: 48.9963512, lng: 21.2525850, name: 'Station 17'},
{lat: 48.9985134, lng: 21.2508423, name: 'Station 18'},
{lat: 49.0085000, lng: 21.2508000, name: 'Station 19'},
{lat: 49.0093000, lng: 21.2528000, name: 'Station 20'},
{lat: 49.0103000, lng: 21.2560000, name: 'Station 21'},
{lat: 49.0112000, lng: 21.2590000, name: 'Station 22'},
{lat: 49.0124000, lng: 21.2620000, name: 'Station 23'},
{lat: 49.0135000, lng: 21.2650000, name: 'Station 24'},
{lat: 49.0149000, lng: 21.2680000, name: 'Station 25'},
{lat: 49.0171000, lng: 21.2710000, name: 'Station 26'},
{lat: 49.0198000, lng: 21.2740000, name: 'Station 27'},
{lat: 49.0305000, lng: 21.3000000, name: 'Station 28'},
// ... as many other stations as you need
];

// Zoom and center map automatically by stations (each station will be in visible map area)
var lngs = stations.map(function(station) { return station.lng; });
var lats = stations.map(function(station) { return station.lat; });
map.fitBounds({
west: Math.min.apply(null, lngs),
east: Math.max.apply(null, lngs),
north: Math.min.apply(null, lats),
south: Math.max.apply(null, lats),
});

// Show stations on the map as markers
for (var i = 0; i < stations.length; i++) {
new google.maps.Marker({
position: stations[i],
map: map,
title: stations[i].name
});
}

// Divide route to several parts because max stations limit is 25 (23 waypoints + 1 origin + 1 destination)
for (var i = 0, parts = , max = 25 - 1; i < stations.length; i = i + max)
parts.push(stations.slice(i, i + max + 1));

// Service callback to process service results
var service_callback = function(response, status) {
if (status != 'OK') {
console.log('Directions request failed due to ' + status);
return;
}
var renderer = new google.maps.DirectionsRenderer;
renderer.setMap(map);
renderer.setOptions({ suppressMarkers: true, preserveViewport: true });
renderer.setDirections(response);
};

// Send requests to service to get route (for stations count <= 25 only one request will be sent)
for (var i = 0; i < parts.length; i++) {
// Waypoints does not include first station (origin) and last station (destination)
var waypoints = ;
for (var j = 1; j < parts[i].length - 1; j++)
waypoints.push({location: parts[i][j], stopover: false});
// Service options
var service_options = {
origin: parts[i][0],
destination: parts[i][parts[i].length - 1],
waypoints: waypoints,
travelMode: 'WALKING'
};
// Send request
service.route(service_options, service_callback);
}
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"></script>


You can see screen here.



fiddle with show/hide line buttons






share|improve this answer



















  • 1




    @Tenz store reference to var map and var renderer somewhere to be accessible later. E.g.: window.gMap = map; and if (!window.gRenderers) { window.renderers = ; } window.gRenderers.push(renderer); and later to hide/unhide line use for (var r of gRenderers) r.setMap(null); to hide and for (var r of gRenderers) r.setMap(gMap); to show. Btw to hide line immediately by default add suppressPolylines = true to renderer.setOptions({}). Unfortunatelly setting suppressPolylines later does not work (only on initialization) so we use must use setMap() hack.
    – mikep
    Mar 19 '18 at 9:04





















0














Here is an hack to use more than 8 waypoints.Please checkout my solution for that.



Drawing roadmap with more than 8 waypoints using Google directions API






share|improve this answer































    0














    There is a easy around this solution.



    Keep the waypoints in array based on the distance threshold value and keep adding them. Once you reach the limit of 8 values in the array, assign the 1st position(Origin) of the waypoint array to a new waypoint array .... Assign the last waypoint to the new waypoint as the 2nd element... now replace the old waypoint array with this new one and continue.



    Watever you do the waypoints will never cross more than 8 values and it will keep track of the route taken to map it out(Unless the journey is way too long)



    var addWaypoint = function(point) {
    if($scope.waypoints.length > 8){
    var temp = ;
    temp.push($scope.waypoints[0]); //Start Point
    temp.push($scope.waypoints[7]); //Last point
    temp.push(point); //New point
    $scope.waypoints = temp; //Replace the old object with this new one
    }
    else
    $scope.waypoints.push(point);
    }





    share|improve this answer





























      -2














      The code below in C# calculates how many calls you will make Google Directions API and how many waypoints on each iteration. You can modify the Modmin to change the minimum waypoints that you want on your last iteration.



      For example if you have totalWaypoints.Count = 97:



      97 Mod 23 = 5, in this case i want a Modmin greater than 5, so i will calculate again with a lower waypointsByIteration;



      97 Mod 22 = 9, (9 > Modmin) , OK;



      iterations = ((97 - (97 % 22)) / (22)) + 1 = 5;



      On the last iteration waypointsByIteration will be the residue.



              var iterations = 1;//Number of iterations
      var waypointsByIteration = 23;//Number of waypoints by iteration
      var modMin = 5;//Minimum of Waypoints in the last iteration
      var residue = 0;//Residue of the division (totalWaypoints % waypointsByIteration)

      if (totalWaypoints.Count <= waypointsByIteration)
      {
      waypointsByIteration = totalWaypoints.Count;
      }
      else
      {
      while (totalWaypoints.Count % waypointsByIteration < modMin)
      {
      waypointsByIteration--;
      }

      //Calculate number of waypoints by iteracoes
      iterations = ((totalWaypoints.Count - (totalWaypoints.Count % waypointsByIteration)) / (waypointsByIteration)) + 1;
      }

      residue = totalWaypoints % waypointsByIteration;

      for(i=0;i<iterations;i++)
      {
      //If it is the last index, the waypointsByIteration will be the residue
      if(i == iteration - 1)
      {
      waypointsByIteration = residue;
      }
      }





      share|improve this answer




















        protected by Community Sep 19 '17 at 14:06



        Thank you for your interest in this question.
        Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



        Would you like to answer one of these unanswered questions instead?














        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        15














        You are correct the premier pricing is rather expensive starting at $10,000, last time I spoke with a google rep on the phone.



        I found a workaround that I put into place to in a way bypasss the 8 waypoints limitation. I was able to make it work.



        I did this by receiving my waypoints and breaking them up into different routes but drawing them together as the same route.



        Example being if there were 30 waypoints needed I would draw 4 lines, but with the same color etc. So, basically you cut the waypoints into different routes calling the directions renderer each time as if it was a different route. The key is after the first route that the next route has to start with the last waypoint of the previous route (this makes sure that the route lines are connected to each other)



        It works but you need to write a lot more code than what you would if you had a premier account, and you are calling for directions a lot more in this instance.



        I have searched and thought about other ways to do this without having a premier account and have failed.



        Although, I when speaking with google they did say that they intended on creating a tiered structure of payment for customers with different wants/needs. For instance, if a customer just needed more waypts and not a bunch more direction requests.



        Hope this helps, as it worked for me in a practice application.






        share|improve this answer

















        • 20




          Google really think everyone is as rich as them
          – Pacerier
          Jan 10 '12 at 19:45






        • 1




          This works for me and I know of several others that use it this way. It only works if you aren't hitting it thousands of times a day.
          – Bill Blankenship
          Jul 2 '12 at 13:46










        • What is working for you? Where is the hack? In the direction api a direction has 23 waypoints but what is the hack? It's like in my answer is about the loading of the library not how many waypoints of direction you fire on Google????
          – Bytemain
          Jul 2 '12 at 14:23










        • Please read this link. We are not talking about tsp problem. We are talking about waypoint limits via the directions renderer and how to plot a route that has more than the maximum allowed via this usage limit that google provides. developers.google.com/maps/documentation/business/…
          – Bill Blankenship
          Jul 2 '12 at 14:55












        • If you don't understand this please contact google their is a phone number on there.
          – Bill Blankenship
          Jul 2 '12 at 14:57
















        15














        You are correct the premier pricing is rather expensive starting at $10,000, last time I spoke with a google rep on the phone.



        I found a workaround that I put into place to in a way bypasss the 8 waypoints limitation. I was able to make it work.



        I did this by receiving my waypoints and breaking them up into different routes but drawing them together as the same route.



        Example being if there were 30 waypoints needed I would draw 4 lines, but with the same color etc. So, basically you cut the waypoints into different routes calling the directions renderer each time as if it was a different route. The key is after the first route that the next route has to start with the last waypoint of the previous route (this makes sure that the route lines are connected to each other)



        It works but you need to write a lot more code than what you would if you had a premier account, and you are calling for directions a lot more in this instance.



        I have searched and thought about other ways to do this without having a premier account and have failed.



        Although, I when speaking with google they did say that they intended on creating a tiered structure of payment for customers with different wants/needs. For instance, if a customer just needed more waypts and not a bunch more direction requests.



        Hope this helps, as it worked for me in a practice application.






        share|improve this answer

















        • 20




          Google really think everyone is as rich as them
          – Pacerier
          Jan 10 '12 at 19:45






        • 1




          This works for me and I know of several others that use it this way. It only works if you aren't hitting it thousands of times a day.
          – Bill Blankenship
          Jul 2 '12 at 13:46










        • What is working for you? Where is the hack? In the direction api a direction has 23 waypoints but what is the hack? It's like in my answer is about the loading of the library not how many waypoints of direction you fire on Google????
          – Bytemain
          Jul 2 '12 at 14:23










        • Please read this link. We are not talking about tsp problem. We are talking about waypoint limits via the directions renderer and how to plot a route that has more than the maximum allowed via this usage limit that google provides. developers.google.com/maps/documentation/business/…
          – Bill Blankenship
          Jul 2 '12 at 14:55












        • If you don't understand this please contact google their is a phone number on there.
          – Bill Blankenship
          Jul 2 '12 at 14:57














        15












        15








        15






        You are correct the premier pricing is rather expensive starting at $10,000, last time I spoke with a google rep on the phone.



        I found a workaround that I put into place to in a way bypasss the 8 waypoints limitation. I was able to make it work.



        I did this by receiving my waypoints and breaking them up into different routes but drawing them together as the same route.



        Example being if there were 30 waypoints needed I would draw 4 lines, but with the same color etc. So, basically you cut the waypoints into different routes calling the directions renderer each time as if it was a different route. The key is after the first route that the next route has to start with the last waypoint of the previous route (this makes sure that the route lines are connected to each other)



        It works but you need to write a lot more code than what you would if you had a premier account, and you are calling for directions a lot more in this instance.



        I have searched and thought about other ways to do this without having a premier account and have failed.



        Although, I when speaking with google they did say that they intended on creating a tiered structure of payment for customers with different wants/needs. For instance, if a customer just needed more waypts and not a bunch more direction requests.



        Hope this helps, as it worked for me in a practice application.






        share|improve this answer












        You are correct the premier pricing is rather expensive starting at $10,000, last time I spoke with a google rep on the phone.



        I found a workaround that I put into place to in a way bypasss the 8 waypoints limitation. I was able to make it work.



        I did this by receiving my waypoints and breaking them up into different routes but drawing them together as the same route.



        Example being if there were 30 waypoints needed I would draw 4 lines, but with the same color etc. So, basically you cut the waypoints into different routes calling the directions renderer each time as if it was a different route. The key is after the first route that the next route has to start with the last waypoint of the previous route (this makes sure that the route lines are connected to each other)



        It works but you need to write a lot more code than what you would if you had a premier account, and you are calling for directions a lot more in this instance.



        I have searched and thought about other ways to do this without having a premier account and have failed.



        Although, I when speaking with google they did say that they intended on creating a tiered structure of payment for customers with different wants/needs. For instance, if a customer just needed more waypts and not a bunch more direction requests.



        Hope this helps, as it worked for me in a practice application.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 9 '12 at 23:43









        Bill Blankenship

        2,18243665




        2,18243665








        • 20




          Google really think everyone is as rich as them
          – Pacerier
          Jan 10 '12 at 19:45






        • 1




          This works for me and I know of several others that use it this way. It only works if you aren't hitting it thousands of times a day.
          – Bill Blankenship
          Jul 2 '12 at 13:46










        • What is working for you? Where is the hack? In the direction api a direction has 23 waypoints but what is the hack? It's like in my answer is about the loading of the library not how many waypoints of direction you fire on Google????
          – Bytemain
          Jul 2 '12 at 14:23










        • Please read this link. We are not talking about tsp problem. We are talking about waypoint limits via the directions renderer and how to plot a route that has more than the maximum allowed via this usage limit that google provides. developers.google.com/maps/documentation/business/…
          – Bill Blankenship
          Jul 2 '12 at 14:55












        • If you don't understand this please contact google their is a phone number on there.
          – Bill Blankenship
          Jul 2 '12 at 14:57














        • 20




          Google really think everyone is as rich as them
          – Pacerier
          Jan 10 '12 at 19:45






        • 1




          This works for me and I know of several others that use it this way. It only works if you aren't hitting it thousands of times a day.
          – Bill Blankenship
          Jul 2 '12 at 13:46










        • What is working for you? Where is the hack? In the direction api a direction has 23 waypoints but what is the hack? It's like in my answer is about the loading of the library not how many waypoints of direction you fire on Google????
          – Bytemain
          Jul 2 '12 at 14:23










        • Please read this link. We are not talking about tsp problem. We are talking about waypoint limits via the directions renderer and how to plot a route that has more than the maximum allowed via this usage limit that google provides. developers.google.com/maps/documentation/business/…
          – Bill Blankenship
          Jul 2 '12 at 14:55












        • If you don't understand this please contact google their is a phone number on there.
          – Bill Blankenship
          Jul 2 '12 at 14:57








        20




        20




        Google really think everyone is as rich as them
        – Pacerier
        Jan 10 '12 at 19:45




        Google really think everyone is as rich as them
        – Pacerier
        Jan 10 '12 at 19:45




        1




        1




        This works for me and I know of several others that use it this way. It only works if you aren't hitting it thousands of times a day.
        – Bill Blankenship
        Jul 2 '12 at 13:46




        This works for me and I know of several others that use it this way. It only works if you aren't hitting it thousands of times a day.
        – Bill Blankenship
        Jul 2 '12 at 13:46












        What is working for you? Where is the hack? In the direction api a direction has 23 waypoints but what is the hack? It's like in my answer is about the loading of the library not how many waypoints of direction you fire on Google????
        – Bytemain
        Jul 2 '12 at 14:23




        What is working for you? Where is the hack? In the direction api a direction has 23 waypoints but what is the hack? It's like in my answer is about the loading of the library not how many waypoints of direction you fire on Google????
        – Bytemain
        Jul 2 '12 at 14:23












        Please read this link. We are not talking about tsp problem. We are talking about waypoint limits via the directions renderer and how to plot a route that has more than the maximum allowed via this usage limit that google provides. developers.google.com/maps/documentation/business/…
        – Bill Blankenship
        Jul 2 '12 at 14:55






        Please read this link. We are not talking about tsp problem. We are talking about waypoint limits via the directions renderer and how to plot a route that has more than the maximum allowed via this usage limit that google provides. developers.google.com/maps/documentation/business/…
        – Bill Blankenship
        Jul 2 '12 at 14:55














        If you don't understand this please contact google their is a phone number on there.
        – Bill Blankenship
        Jul 2 '12 at 14:57




        If you don't understand this please contact google their is a phone number on there.
        – Bill Blankenship
        Jul 2 '12 at 14:57













        10

















        function initMap() {
        var service = new google.maps.DirectionsService;
        var map = new google.maps.Map(document.getElementById('map'));

        // list of points
        var stations = [
        {lat: 48.9812840, lng: 21.2171920, name: 'Station 1'},
        {lat: 48.9832841, lng: 21.2176398, name: 'Station 2'},
        {lat: 48.9856443, lng: 21.2209088, name: 'Station 3'},
        {lat: 48.9861461, lng: 21.2261563, name: 'Station 4'},
        {lat: 48.9874682, lng: 21.2294855, name: 'Station 5'},
        {lat: 48.9909244, lng: 21.2295512, name: 'Station 6'},
        {lat: 48.9928871, lng: 21.2292352, name: 'Station 7'},
        {lat: 48.9921334, lng: 21.2246742, name: 'Station 8'},
        {lat: 48.9943196, lng: 21.2234792, name: 'Station 9'},
        {lat: 48.9966345, lng: 21.2221262, name: 'Station 10'},
        {lat: 48.9981191, lng: 21.2271386, name: 'Station 11'},
        {lat: 49.0009168, lng: 21.2359527, name: 'Station 12'},
        {lat: 49.0017950, lng: 21.2392890, name: 'Station 13'},
        {lat: 48.9991912, lng: 21.2398272, name: 'Station 14'},
        {lat: 48.9959850, lng: 21.2418410, name: 'Station 15'},
        {lat: 48.9931772, lng: 21.2453901, name: 'Station 16'},
        {lat: 48.9963512, lng: 21.2525850, name: 'Station 17'},
        {lat: 48.9985134, lng: 21.2508423, name: 'Station 18'},
        {lat: 49.0085000, lng: 21.2508000, name: 'Station 19'},
        {lat: 49.0093000, lng: 21.2528000, name: 'Station 20'},
        {lat: 49.0103000, lng: 21.2560000, name: 'Station 21'},
        {lat: 49.0112000, lng: 21.2590000, name: 'Station 22'},
        {lat: 49.0124000, lng: 21.2620000, name: 'Station 23'},
        {lat: 49.0135000, lng: 21.2650000, name: 'Station 24'},
        {lat: 49.0149000, lng: 21.2680000, name: 'Station 25'},
        {lat: 49.0171000, lng: 21.2710000, name: 'Station 26'},
        {lat: 49.0198000, lng: 21.2740000, name: 'Station 27'},
        {lat: 49.0305000, lng: 21.3000000, name: 'Station 28'},
        ];

        // Zoom and center map automatically by stations (each station will be in visible map area)
        var lngs = stations.map(function(station) { return station.lng; });
        var lats = stations.map(function(station) { return station.lat; });
        map.fitBounds({
        west: Math.min.apply(null, lngs),
        east: Math.max.apply(null, lngs),
        north: Math.min.apply(null, lats),
        south: Math.max.apply(null, lats),
        });

        // Show stations on the map as markers
        for (var i = 0; i < stations.length; i++) {
        if (!stations[i].name)
        continue;
        new google.maps.Marker({
        position: stations[i],
        map: map,
        title: stations[i].name
        });
        }

        // Divide route to several parts because max stations limit is 25 (23 waypoints + 1 origin + 1 destination)
        for (var i = 0, parts = , max = 8 - 1; i < stations.length; i = i + max)
        parts.push(stations.slice(i, i + max + 1));

        // Callback function to process service results
        var service_callback = function(response, status) {
        if (status != 'OK') {
        console.log('Directions request failed due to ' + status);
        return;
        }
        var renderer = new google.maps.DirectionsRenderer;
        renderer.setMap(map);
        renderer.setOptions({ suppressMarkers: true, preserveViewport: true });
        renderer.setDirections(response);
        };

        // Send requests to service to get route (for stations count <= 25 only one request will be sent)
        for (var i = 0; i < parts.length; i++) {
        // Waypoints does not include first station (origin) and last station (destination)
        var waypoints = ;
        for (var j = 1; j < parts[i].length - 1; j++)
        waypoints.push({location: parts[i][j], stopover: false});
        // Service options
        var service_options = {
        origin: parts[i][0],
        destination: parts[i][parts[i].length - 1],
        waypoints: waypoints,
        travelMode: 'WALKING'
        };
        // Send request
        service.route(service_options, service_callback);
        }
        }

        html, body {
        height: 100%;
        margin: 0;
        padding: 0;
        }
        #map {
        height: 100%;
        width: 100%;
        height: 100%;
        }

        <div id="map"></div>

        <!-- without API KEY set variable "max" to 8 -->
        <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script>

        <!-- with API KEY set variable "max" to 25 -->
        <!-- <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap&key=YOUR_API_KEY"></script>-->





        With following code you can use as many waypoints as you need and you will never get error MAX_WAYPOINTS_EXCEEDED. Do not forget to replace "YOUR_API_KEY" to your API KEY or remove &key=YOUR_API_KEY from google API URL and set variable "max" to 8 (max = 25 when using API KEY, max = 8 when not using API KEY).





        <style>
        html, body { height: 100%; margin: 0; padding: 0; }
        #map { height: 100%; width: 100%; height: 100%; }
        </style>
        <div id="map"></div>
        <script>
        function initMap() {
        var service = new google.maps.DirectionsService;
        var map = new google.maps.Map(document.getElementById('map'));

        // list of points
        var stations = [
        {lat: 48.9812840, lng: 21.2171920, name: 'Station 1'},
        {lat: 48.9832841, lng: 21.2176398, name: 'Station 2'},
        {lat: 48.9856443, lng: 21.2209088, name: 'Station 3'},
        {lat: 48.9861461, lng: 21.2261563, name: 'Station 4'},
        {lat: 48.9874682, lng: 21.2294855, name: 'Station 5'},
        {lat: 48.9909244, lng: 21.2295512, name: 'Station 6'},
        {lat: 48.9928871, lng: 21.2292352, name: 'Station 7'},
        {lat: 48.9921334, lng: 21.2246742, name: 'Station 8'},
        {lat: 48.9943196, lng: 21.2234792, name: 'Station 9'},
        {lat: 48.9966345, lng: 21.2221262, name: 'Station 10'},
        {lat: 48.9981191, lng: 21.2271386, name: 'Station 11'},
        {lat: 49.0009168, lng: 21.2359527, name: 'Station 12'},
        {lat: 49.0017950, lng: 21.2392890, name: 'Station 13'},
        {lat: 48.9991912, lng: 21.2398272, name: 'Station 14'},
        {lat: 48.9959850, lng: 21.2418410, name: 'Station 15'},
        {lat: 48.9931772, lng: 21.2453901, name: 'Station 16'},
        {lat: 48.9963512, lng: 21.2525850, name: 'Station 17'},
        {lat: 48.9985134, lng: 21.2508423, name: 'Station 18'},
        {lat: 49.0085000, lng: 21.2508000, name: 'Station 19'},
        {lat: 49.0093000, lng: 21.2528000, name: 'Station 20'},
        {lat: 49.0103000, lng: 21.2560000, name: 'Station 21'},
        {lat: 49.0112000, lng: 21.2590000, name: 'Station 22'},
        {lat: 49.0124000, lng: 21.2620000, name: 'Station 23'},
        {lat: 49.0135000, lng: 21.2650000, name: 'Station 24'},
        {lat: 49.0149000, lng: 21.2680000, name: 'Station 25'},
        {lat: 49.0171000, lng: 21.2710000, name: 'Station 26'},
        {lat: 49.0198000, lng: 21.2740000, name: 'Station 27'},
        {lat: 49.0305000, lng: 21.3000000, name: 'Station 28'},
        // ... as many other stations as you need
        ];

        // Zoom and center map automatically by stations (each station will be in visible map area)
        var lngs = stations.map(function(station) { return station.lng; });
        var lats = stations.map(function(station) { return station.lat; });
        map.fitBounds({
        west: Math.min.apply(null, lngs),
        east: Math.max.apply(null, lngs),
        north: Math.min.apply(null, lats),
        south: Math.max.apply(null, lats),
        });

        // Show stations on the map as markers
        for (var i = 0; i < stations.length; i++) {
        new google.maps.Marker({
        position: stations[i],
        map: map,
        title: stations[i].name
        });
        }

        // Divide route to several parts because max stations limit is 25 (23 waypoints + 1 origin + 1 destination)
        for (var i = 0, parts = , max = 25 - 1; i < stations.length; i = i + max)
        parts.push(stations.slice(i, i + max + 1));

        // Service callback to process service results
        var service_callback = function(response, status) {
        if (status != 'OK') {
        console.log('Directions request failed due to ' + status);
        return;
        }
        var renderer = new google.maps.DirectionsRenderer;
        renderer.setMap(map);
        renderer.setOptions({ suppressMarkers: true, preserveViewport: true });
        renderer.setDirections(response);
        };

        // Send requests to service to get route (for stations count <= 25 only one request will be sent)
        for (var i = 0; i < parts.length; i++) {
        // Waypoints does not include first station (origin) and last station (destination)
        var waypoints = ;
        for (var j = 1; j < parts[i].length - 1; j++)
        waypoints.push({location: parts[i][j], stopover: false});
        // Service options
        var service_options = {
        origin: parts[i][0],
        destination: parts[i][parts[i].length - 1],
        waypoints: waypoints,
        travelMode: 'WALKING'
        };
        // Send request
        service.route(service_options, service_callback);
        }
        }
        </script>
        <script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"></script>


        You can see screen here.



        fiddle with show/hide line buttons






        share|improve this answer



















        • 1




          @Tenz store reference to var map and var renderer somewhere to be accessible later. E.g.: window.gMap = map; and if (!window.gRenderers) { window.renderers = ; } window.gRenderers.push(renderer); and later to hide/unhide line use for (var r of gRenderers) r.setMap(null); to hide and for (var r of gRenderers) r.setMap(gMap); to show. Btw to hide line immediately by default add suppressPolylines = true to renderer.setOptions({}). Unfortunatelly setting suppressPolylines later does not work (only on initialization) so we use must use setMap() hack.
          – mikep
          Mar 19 '18 at 9:04


















        10

















        function initMap() {
        var service = new google.maps.DirectionsService;
        var map = new google.maps.Map(document.getElementById('map'));

        // list of points
        var stations = [
        {lat: 48.9812840, lng: 21.2171920, name: 'Station 1'},
        {lat: 48.9832841, lng: 21.2176398, name: 'Station 2'},
        {lat: 48.9856443, lng: 21.2209088, name: 'Station 3'},
        {lat: 48.9861461, lng: 21.2261563, name: 'Station 4'},
        {lat: 48.9874682, lng: 21.2294855, name: 'Station 5'},
        {lat: 48.9909244, lng: 21.2295512, name: 'Station 6'},
        {lat: 48.9928871, lng: 21.2292352, name: 'Station 7'},
        {lat: 48.9921334, lng: 21.2246742, name: 'Station 8'},
        {lat: 48.9943196, lng: 21.2234792, name: 'Station 9'},
        {lat: 48.9966345, lng: 21.2221262, name: 'Station 10'},
        {lat: 48.9981191, lng: 21.2271386, name: 'Station 11'},
        {lat: 49.0009168, lng: 21.2359527, name: 'Station 12'},
        {lat: 49.0017950, lng: 21.2392890, name: 'Station 13'},
        {lat: 48.9991912, lng: 21.2398272, name: 'Station 14'},
        {lat: 48.9959850, lng: 21.2418410, name: 'Station 15'},
        {lat: 48.9931772, lng: 21.2453901, name: 'Station 16'},
        {lat: 48.9963512, lng: 21.2525850, name: 'Station 17'},
        {lat: 48.9985134, lng: 21.2508423, name: 'Station 18'},
        {lat: 49.0085000, lng: 21.2508000, name: 'Station 19'},
        {lat: 49.0093000, lng: 21.2528000, name: 'Station 20'},
        {lat: 49.0103000, lng: 21.2560000, name: 'Station 21'},
        {lat: 49.0112000, lng: 21.2590000, name: 'Station 22'},
        {lat: 49.0124000, lng: 21.2620000, name: 'Station 23'},
        {lat: 49.0135000, lng: 21.2650000, name: 'Station 24'},
        {lat: 49.0149000, lng: 21.2680000, name: 'Station 25'},
        {lat: 49.0171000, lng: 21.2710000, name: 'Station 26'},
        {lat: 49.0198000, lng: 21.2740000, name: 'Station 27'},
        {lat: 49.0305000, lng: 21.3000000, name: 'Station 28'},
        ];

        // Zoom and center map automatically by stations (each station will be in visible map area)
        var lngs = stations.map(function(station) { return station.lng; });
        var lats = stations.map(function(station) { return station.lat; });
        map.fitBounds({
        west: Math.min.apply(null, lngs),
        east: Math.max.apply(null, lngs),
        north: Math.min.apply(null, lats),
        south: Math.max.apply(null, lats),
        });

        // Show stations on the map as markers
        for (var i = 0; i < stations.length; i++) {
        if (!stations[i].name)
        continue;
        new google.maps.Marker({
        position: stations[i],
        map: map,
        title: stations[i].name
        });
        }

        // Divide route to several parts because max stations limit is 25 (23 waypoints + 1 origin + 1 destination)
        for (var i = 0, parts = , max = 8 - 1; i < stations.length; i = i + max)
        parts.push(stations.slice(i, i + max + 1));

        // Callback function to process service results
        var service_callback = function(response, status) {
        if (status != 'OK') {
        console.log('Directions request failed due to ' + status);
        return;
        }
        var renderer = new google.maps.DirectionsRenderer;
        renderer.setMap(map);
        renderer.setOptions({ suppressMarkers: true, preserveViewport: true });
        renderer.setDirections(response);
        };

        // Send requests to service to get route (for stations count <= 25 only one request will be sent)
        for (var i = 0; i < parts.length; i++) {
        // Waypoints does not include first station (origin) and last station (destination)
        var waypoints = ;
        for (var j = 1; j < parts[i].length - 1; j++)
        waypoints.push({location: parts[i][j], stopover: false});
        // Service options
        var service_options = {
        origin: parts[i][0],
        destination: parts[i][parts[i].length - 1],
        waypoints: waypoints,
        travelMode: 'WALKING'
        };
        // Send request
        service.route(service_options, service_callback);
        }
        }

        html, body {
        height: 100%;
        margin: 0;
        padding: 0;
        }
        #map {
        height: 100%;
        width: 100%;
        height: 100%;
        }

        <div id="map"></div>

        <!-- without API KEY set variable "max" to 8 -->
        <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script>

        <!-- with API KEY set variable "max" to 25 -->
        <!-- <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap&key=YOUR_API_KEY"></script>-->





        With following code you can use as many waypoints as you need and you will never get error MAX_WAYPOINTS_EXCEEDED. Do not forget to replace "YOUR_API_KEY" to your API KEY or remove &key=YOUR_API_KEY from google API URL and set variable "max" to 8 (max = 25 when using API KEY, max = 8 when not using API KEY).





        <style>
        html, body { height: 100%; margin: 0; padding: 0; }
        #map { height: 100%; width: 100%; height: 100%; }
        </style>
        <div id="map"></div>
        <script>
        function initMap() {
        var service = new google.maps.DirectionsService;
        var map = new google.maps.Map(document.getElementById('map'));

        // list of points
        var stations = [
        {lat: 48.9812840, lng: 21.2171920, name: 'Station 1'},
        {lat: 48.9832841, lng: 21.2176398, name: 'Station 2'},
        {lat: 48.9856443, lng: 21.2209088, name: 'Station 3'},
        {lat: 48.9861461, lng: 21.2261563, name: 'Station 4'},
        {lat: 48.9874682, lng: 21.2294855, name: 'Station 5'},
        {lat: 48.9909244, lng: 21.2295512, name: 'Station 6'},
        {lat: 48.9928871, lng: 21.2292352, name: 'Station 7'},
        {lat: 48.9921334, lng: 21.2246742, name: 'Station 8'},
        {lat: 48.9943196, lng: 21.2234792, name: 'Station 9'},
        {lat: 48.9966345, lng: 21.2221262, name: 'Station 10'},
        {lat: 48.9981191, lng: 21.2271386, name: 'Station 11'},
        {lat: 49.0009168, lng: 21.2359527, name: 'Station 12'},
        {lat: 49.0017950, lng: 21.2392890, name: 'Station 13'},
        {lat: 48.9991912, lng: 21.2398272, name: 'Station 14'},
        {lat: 48.9959850, lng: 21.2418410, name: 'Station 15'},
        {lat: 48.9931772, lng: 21.2453901, name: 'Station 16'},
        {lat: 48.9963512, lng: 21.2525850, name: 'Station 17'},
        {lat: 48.9985134, lng: 21.2508423, name: 'Station 18'},
        {lat: 49.0085000, lng: 21.2508000, name: 'Station 19'},
        {lat: 49.0093000, lng: 21.2528000, name: 'Station 20'},
        {lat: 49.0103000, lng: 21.2560000, name: 'Station 21'},
        {lat: 49.0112000, lng: 21.2590000, name: 'Station 22'},
        {lat: 49.0124000, lng: 21.2620000, name: 'Station 23'},
        {lat: 49.0135000, lng: 21.2650000, name: 'Station 24'},
        {lat: 49.0149000, lng: 21.2680000, name: 'Station 25'},
        {lat: 49.0171000, lng: 21.2710000, name: 'Station 26'},
        {lat: 49.0198000, lng: 21.2740000, name: 'Station 27'},
        {lat: 49.0305000, lng: 21.3000000, name: 'Station 28'},
        // ... as many other stations as you need
        ];

        // Zoom and center map automatically by stations (each station will be in visible map area)
        var lngs = stations.map(function(station) { return station.lng; });
        var lats = stations.map(function(station) { return station.lat; });
        map.fitBounds({
        west: Math.min.apply(null, lngs),
        east: Math.max.apply(null, lngs),
        north: Math.min.apply(null, lats),
        south: Math.max.apply(null, lats),
        });

        // Show stations on the map as markers
        for (var i = 0; i < stations.length; i++) {
        new google.maps.Marker({
        position: stations[i],
        map: map,
        title: stations[i].name
        });
        }

        // Divide route to several parts because max stations limit is 25 (23 waypoints + 1 origin + 1 destination)
        for (var i = 0, parts = , max = 25 - 1; i < stations.length; i = i + max)
        parts.push(stations.slice(i, i + max + 1));

        // Service callback to process service results
        var service_callback = function(response, status) {
        if (status != 'OK') {
        console.log('Directions request failed due to ' + status);
        return;
        }
        var renderer = new google.maps.DirectionsRenderer;
        renderer.setMap(map);
        renderer.setOptions({ suppressMarkers: true, preserveViewport: true });
        renderer.setDirections(response);
        };

        // Send requests to service to get route (for stations count <= 25 only one request will be sent)
        for (var i = 0; i < parts.length; i++) {
        // Waypoints does not include first station (origin) and last station (destination)
        var waypoints = ;
        for (var j = 1; j < parts[i].length - 1; j++)
        waypoints.push({location: parts[i][j], stopover: false});
        // Service options
        var service_options = {
        origin: parts[i][0],
        destination: parts[i][parts[i].length - 1],
        waypoints: waypoints,
        travelMode: 'WALKING'
        };
        // Send request
        service.route(service_options, service_callback);
        }
        }
        </script>
        <script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"></script>


        You can see screen here.



        fiddle with show/hide line buttons






        share|improve this answer



















        • 1




          @Tenz store reference to var map and var renderer somewhere to be accessible later. E.g.: window.gMap = map; and if (!window.gRenderers) { window.renderers = ; } window.gRenderers.push(renderer); and later to hide/unhide line use for (var r of gRenderers) r.setMap(null); to hide and for (var r of gRenderers) r.setMap(gMap); to show. Btw to hide line immediately by default add suppressPolylines = true to renderer.setOptions({}). Unfortunatelly setting suppressPolylines later does not work (only on initialization) so we use must use setMap() hack.
          – mikep
          Mar 19 '18 at 9:04
















        10












        10








        10









        function initMap() {
        var service = new google.maps.DirectionsService;
        var map = new google.maps.Map(document.getElementById('map'));

        // list of points
        var stations = [
        {lat: 48.9812840, lng: 21.2171920, name: 'Station 1'},
        {lat: 48.9832841, lng: 21.2176398, name: 'Station 2'},
        {lat: 48.9856443, lng: 21.2209088, name: 'Station 3'},
        {lat: 48.9861461, lng: 21.2261563, name: 'Station 4'},
        {lat: 48.9874682, lng: 21.2294855, name: 'Station 5'},
        {lat: 48.9909244, lng: 21.2295512, name: 'Station 6'},
        {lat: 48.9928871, lng: 21.2292352, name: 'Station 7'},
        {lat: 48.9921334, lng: 21.2246742, name: 'Station 8'},
        {lat: 48.9943196, lng: 21.2234792, name: 'Station 9'},
        {lat: 48.9966345, lng: 21.2221262, name: 'Station 10'},
        {lat: 48.9981191, lng: 21.2271386, name: 'Station 11'},
        {lat: 49.0009168, lng: 21.2359527, name: 'Station 12'},
        {lat: 49.0017950, lng: 21.2392890, name: 'Station 13'},
        {lat: 48.9991912, lng: 21.2398272, name: 'Station 14'},
        {lat: 48.9959850, lng: 21.2418410, name: 'Station 15'},
        {lat: 48.9931772, lng: 21.2453901, name: 'Station 16'},
        {lat: 48.9963512, lng: 21.2525850, name: 'Station 17'},
        {lat: 48.9985134, lng: 21.2508423, name: 'Station 18'},
        {lat: 49.0085000, lng: 21.2508000, name: 'Station 19'},
        {lat: 49.0093000, lng: 21.2528000, name: 'Station 20'},
        {lat: 49.0103000, lng: 21.2560000, name: 'Station 21'},
        {lat: 49.0112000, lng: 21.2590000, name: 'Station 22'},
        {lat: 49.0124000, lng: 21.2620000, name: 'Station 23'},
        {lat: 49.0135000, lng: 21.2650000, name: 'Station 24'},
        {lat: 49.0149000, lng: 21.2680000, name: 'Station 25'},
        {lat: 49.0171000, lng: 21.2710000, name: 'Station 26'},
        {lat: 49.0198000, lng: 21.2740000, name: 'Station 27'},
        {lat: 49.0305000, lng: 21.3000000, name: 'Station 28'},
        ];

        // Zoom and center map automatically by stations (each station will be in visible map area)
        var lngs = stations.map(function(station) { return station.lng; });
        var lats = stations.map(function(station) { return station.lat; });
        map.fitBounds({
        west: Math.min.apply(null, lngs),
        east: Math.max.apply(null, lngs),
        north: Math.min.apply(null, lats),
        south: Math.max.apply(null, lats),
        });

        // Show stations on the map as markers
        for (var i = 0; i < stations.length; i++) {
        if (!stations[i].name)
        continue;
        new google.maps.Marker({
        position: stations[i],
        map: map,
        title: stations[i].name
        });
        }

        // Divide route to several parts because max stations limit is 25 (23 waypoints + 1 origin + 1 destination)
        for (var i = 0, parts = , max = 8 - 1; i < stations.length; i = i + max)
        parts.push(stations.slice(i, i + max + 1));

        // Callback function to process service results
        var service_callback = function(response, status) {
        if (status != 'OK') {
        console.log('Directions request failed due to ' + status);
        return;
        }
        var renderer = new google.maps.DirectionsRenderer;
        renderer.setMap(map);
        renderer.setOptions({ suppressMarkers: true, preserveViewport: true });
        renderer.setDirections(response);
        };

        // Send requests to service to get route (for stations count <= 25 only one request will be sent)
        for (var i = 0; i < parts.length; i++) {
        // Waypoints does not include first station (origin) and last station (destination)
        var waypoints = ;
        for (var j = 1; j < parts[i].length - 1; j++)
        waypoints.push({location: parts[i][j], stopover: false});
        // Service options
        var service_options = {
        origin: parts[i][0],
        destination: parts[i][parts[i].length - 1],
        waypoints: waypoints,
        travelMode: 'WALKING'
        };
        // Send request
        service.route(service_options, service_callback);
        }
        }

        html, body {
        height: 100%;
        margin: 0;
        padding: 0;
        }
        #map {
        height: 100%;
        width: 100%;
        height: 100%;
        }

        <div id="map"></div>

        <!-- without API KEY set variable "max" to 8 -->
        <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script>

        <!-- with API KEY set variable "max" to 25 -->
        <!-- <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap&key=YOUR_API_KEY"></script>-->





        With following code you can use as many waypoints as you need and you will never get error MAX_WAYPOINTS_EXCEEDED. Do not forget to replace "YOUR_API_KEY" to your API KEY or remove &key=YOUR_API_KEY from google API URL and set variable "max" to 8 (max = 25 when using API KEY, max = 8 when not using API KEY).





        <style>
        html, body { height: 100%; margin: 0; padding: 0; }
        #map { height: 100%; width: 100%; height: 100%; }
        </style>
        <div id="map"></div>
        <script>
        function initMap() {
        var service = new google.maps.DirectionsService;
        var map = new google.maps.Map(document.getElementById('map'));

        // list of points
        var stations = [
        {lat: 48.9812840, lng: 21.2171920, name: 'Station 1'},
        {lat: 48.9832841, lng: 21.2176398, name: 'Station 2'},
        {lat: 48.9856443, lng: 21.2209088, name: 'Station 3'},
        {lat: 48.9861461, lng: 21.2261563, name: 'Station 4'},
        {lat: 48.9874682, lng: 21.2294855, name: 'Station 5'},
        {lat: 48.9909244, lng: 21.2295512, name: 'Station 6'},
        {lat: 48.9928871, lng: 21.2292352, name: 'Station 7'},
        {lat: 48.9921334, lng: 21.2246742, name: 'Station 8'},
        {lat: 48.9943196, lng: 21.2234792, name: 'Station 9'},
        {lat: 48.9966345, lng: 21.2221262, name: 'Station 10'},
        {lat: 48.9981191, lng: 21.2271386, name: 'Station 11'},
        {lat: 49.0009168, lng: 21.2359527, name: 'Station 12'},
        {lat: 49.0017950, lng: 21.2392890, name: 'Station 13'},
        {lat: 48.9991912, lng: 21.2398272, name: 'Station 14'},
        {lat: 48.9959850, lng: 21.2418410, name: 'Station 15'},
        {lat: 48.9931772, lng: 21.2453901, name: 'Station 16'},
        {lat: 48.9963512, lng: 21.2525850, name: 'Station 17'},
        {lat: 48.9985134, lng: 21.2508423, name: 'Station 18'},
        {lat: 49.0085000, lng: 21.2508000, name: 'Station 19'},
        {lat: 49.0093000, lng: 21.2528000, name: 'Station 20'},
        {lat: 49.0103000, lng: 21.2560000, name: 'Station 21'},
        {lat: 49.0112000, lng: 21.2590000, name: 'Station 22'},
        {lat: 49.0124000, lng: 21.2620000, name: 'Station 23'},
        {lat: 49.0135000, lng: 21.2650000, name: 'Station 24'},
        {lat: 49.0149000, lng: 21.2680000, name: 'Station 25'},
        {lat: 49.0171000, lng: 21.2710000, name: 'Station 26'},
        {lat: 49.0198000, lng: 21.2740000, name: 'Station 27'},
        {lat: 49.0305000, lng: 21.3000000, name: 'Station 28'},
        // ... as many other stations as you need
        ];

        // Zoom and center map automatically by stations (each station will be in visible map area)
        var lngs = stations.map(function(station) { return station.lng; });
        var lats = stations.map(function(station) { return station.lat; });
        map.fitBounds({
        west: Math.min.apply(null, lngs),
        east: Math.max.apply(null, lngs),
        north: Math.min.apply(null, lats),
        south: Math.max.apply(null, lats),
        });

        // Show stations on the map as markers
        for (var i = 0; i < stations.length; i++) {
        new google.maps.Marker({
        position: stations[i],
        map: map,
        title: stations[i].name
        });
        }

        // Divide route to several parts because max stations limit is 25 (23 waypoints + 1 origin + 1 destination)
        for (var i = 0, parts = , max = 25 - 1; i < stations.length; i = i + max)
        parts.push(stations.slice(i, i + max + 1));

        // Service callback to process service results
        var service_callback = function(response, status) {
        if (status != 'OK') {
        console.log('Directions request failed due to ' + status);
        return;
        }
        var renderer = new google.maps.DirectionsRenderer;
        renderer.setMap(map);
        renderer.setOptions({ suppressMarkers: true, preserveViewport: true });
        renderer.setDirections(response);
        };

        // Send requests to service to get route (for stations count <= 25 only one request will be sent)
        for (var i = 0; i < parts.length; i++) {
        // Waypoints does not include first station (origin) and last station (destination)
        var waypoints = ;
        for (var j = 1; j < parts[i].length - 1; j++)
        waypoints.push({location: parts[i][j], stopover: false});
        // Service options
        var service_options = {
        origin: parts[i][0],
        destination: parts[i][parts[i].length - 1],
        waypoints: waypoints,
        travelMode: 'WALKING'
        };
        // Send request
        service.route(service_options, service_callback);
        }
        }
        </script>
        <script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"></script>


        You can see screen here.



        fiddle with show/hide line buttons






        share|improve this answer

















        function initMap() {
        var service = new google.maps.DirectionsService;
        var map = new google.maps.Map(document.getElementById('map'));

        // list of points
        var stations = [
        {lat: 48.9812840, lng: 21.2171920, name: 'Station 1'},
        {lat: 48.9832841, lng: 21.2176398, name: 'Station 2'},
        {lat: 48.9856443, lng: 21.2209088, name: 'Station 3'},
        {lat: 48.9861461, lng: 21.2261563, name: 'Station 4'},
        {lat: 48.9874682, lng: 21.2294855, name: 'Station 5'},
        {lat: 48.9909244, lng: 21.2295512, name: 'Station 6'},
        {lat: 48.9928871, lng: 21.2292352, name: 'Station 7'},
        {lat: 48.9921334, lng: 21.2246742, name: 'Station 8'},
        {lat: 48.9943196, lng: 21.2234792, name: 'Station 9'},
        {lat: 48.9966345, lng: 21.2221262, name: 'Station 10'},
        {lat: 48.9981191, lng: 21.2271386, name: 'Station 11'},
        {lat: 49.0009168, lng: 21.2359527, name: 'Station 12'},
        {lat: 49.0017950, lng: 21.2392890, name: 'Station 13'},
        {lat: 48.9991912, lng: 21.2398272, name: 'Station 14'},
        {lat: 48.9959850, lng: 21.2418410, name: 'Station 15'},
        {lat: 48.9931772, lng: 21.2453901, name: 'Station 16'},
        {lat: 48.9963512, lng: 21.2525850, name: 'Station 17'},
        {lat: 48.9985134, lng: 21.2508423, name: 'Station 18'},
        {lat: 49.0085000, lng: 21.2508000, name: 'Station 19'},
        {lat: 49.0093000, lng: 21.2528000, name: 'Station 20'},
        {lat: 49.0103000, lng: 21.2560000, name: 'Station 21'},
        {lat: 49.0112000, lng: 21.2590000, name: 'Station 22'},
        {lat: 49.0124000, lng: 21.2620000, name: 'Station 23'},
        {lat: 49.0135000, lng: 21.2650000, name: 'Station 24'},
        {lat: 49.0149000, lng: 21.2680000, name: 'Station 25'},
        {lat: 49.0171000, lng: 21.2710000, name: 'Station 26'},
        {lat: 49.0198000, lng: 21.2740000, name: 'Station 27'},
        {lat: 49.0305000, lng: 21.3000000, name: 'Station 28'},
        ];

        // Zoom and center map automatically by stations (each station will be in visible map area)
        var lngs = stations.map(function(station) { return station.lng; });
        var lats = stations.map(function(station) { return station.lat; });
        map.fitBounds({
        west: Math.min.apply(null, lngs),
        east: Math.max.apply(null, lngs),
        north: Math.min.apply(null, lats),
        south: Math.max.apply(null, lats),
        });

        // Show stations on the map as markers
        for (var i = 0; i < stations.length; i++) {
        if (!stations[i].name)
        continue;
        new google.maps.Marker({
        position: stations[i],
        map: map,
        title: stations[i].name
        });
        }

        // Divide route to several parts because max stations limit is 25 (23 waypoints + 1 origin + 1 destination)
        for (var i = 0, parts = , max = 8 - 1; i < stations.length; i = i + max)
        parts.push(stations.slice(i, i + max + 1));

        // Callback function to process service results
        var service_callback = function(response, status) {
        if (status != 'OK') {
        console.log('Directions request failed due to ' + status);
        return;
        }
        var renderer = new google.maps.DirectionsRenderer;
        renderer.setMap(map);
        renderer.setOptions({ suppressMarkers: true, preserveViewport: true });
        renderer.setDirections(response);
        };

        // Send requests to service to get route (for stations count <= 25 only one request will be sent)
        for (var i = 0; i < parts.length; i++) {
        // Waypoints does not include first station (origin) and last station (destination)
        var waypoints = ;
        for (var j = 1; j < parts[i].length - 1; j++)
        waypoints.push({location: parts[i][j], stopover: false});
        // Service options
        var service_options = {
        origin: parts[i][0],
        destination: parts[i][parts[i].length - 1],
        waypoints: waypoints,
        travelMode: 'WALKING'
        };
        // Send request
        service.route(service_options, service_callback);
        }
        }

        html, body {
        height: 100%;
        margin: 0;
        padding: 0;
        }
        #map {
        height: 100%;
        width: 100%;
        height: 100%;
        }

        <div id="map"></div>

        <!-- without API KEY set variable "max" to 8 -->
        <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script>

        <!-- with API KEY set variable "max" to 25 -->
        <!-- <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap&key=YOUR_API_KEY"></script>-->





        With following code you can use as many waypoints as you need and you will never get error MAX_WAYPOINTS_EXCEEDED. Do not forget to replace "YOUR_API_KEY" to your API KEY or remove &key=YOUR_API_KEY from google API URL and set variable "max" to 8 (max = 25 when using API KEY, max = 8 when not using API KEY).





        <style>
        html, body { height: 100%; margin: 0; padding: 0; }
        #map { height: 100%; width: 100%; height: 100%; }
        </style>
        <div id="map"></div>
        <script>
        function initMap() {
        var service = new google.maps.DirectionsService;
        var map = new google.maps.Map(document.getElementById('map'));

        // list of points
        var stations = [
        {lat: 48.9812840, lng: 21.2171920, name: 'Station 1'},
        {lat: 48.9832841, lng: 21.2176398, name: 'Station 2'},
        {lat: 48.9856443, lng: 21.2209088, name: 'Station 3'},
        {lat: 48.9861461, lng: 21.2261563, name: 'Station 4'},
        {lat: 48.9874682, lng: 21.2294855, name: 'Station 5'},
        {lat: 48.9909244, lng: 21.2295512, name: 'Station 6'},
        {lat: 48.9928871, lng: 21.2292352, name: 'Station 7'},
        {lat: 48.9921334, lng: 21.2246742, name: 'Station 8'},
        {lat: 48.9943196, lng: 21.2234792, name: 'Station 9'},
        {lat: 48.9966345, lng: 21.2221262, name: 'Station 10'},
        {lat: 48.9981191, lng: 21.2271386, name: 'Station 11'},
        {lat: 49.0009168, lng: 21.2359527, name: 'Station 12'},
        {lat: 49.0017950, lng: 21.2392890, name: 'Station 13'},
        {lat: 48.9991912, lng: 21.2398272, name: 'Station 14'},
        {lat: 48.9959850, lng: 21.2418410, name: 'Station 15'},
        {lat: 48.9931772, lng: 21.2453901, name: 'Station 16'},
        {lat: 48.9963512, lng: 21.2525850, name: 'Station 17'},
        {lat: 48.9985134, lng: 21.2508423, name: 'Station 18'},
        {lat: 49.0085000, lng: 21.2508000, name: 'Station 19'},
        {lat: 49.0093000, lng: 21.2528000, name: 'Station 20'},
        {lat: 49.0103000, lng: 21.2560000, name: 'Station 21'},
        {lat: 49.0112000, lng: 21.2590000, name: 'Station 22'},
        {lat: 49.0124000, lng: 21.2620000, name: 'Station 23'},
        {lat: 49.0135000, lng: 21.2650000, name: 'Station 24'},
        {lat: 49.0149000, lng: 21.2680000, name: 'Station 25'},
        {lat: 49.0171000, lng: 21.2710000, name: 'Station 26'},
        {lat: 49.0198000, lng: 21.2740000, name: 'Station 27'},
        {lat: 49.0305000, lng: 21.3000000, name: 'Station 28'},
        // ... as many other stations as you need
        ];

        // Zoom and center map automatically by stations (each station will be in visible map area)
        var lngs = stations.map(function(station) { return station.lng; });
        var lats = stations.map(function(station) { return station.lat; });
        map.fitBounds({
        west: Math.min.apply(null, lngs),
        east: Math.max.apply(null, lngs),
        north: Math.min.apply(null, lats),
        south: Math.max.apply(null, lats),
        });

        // Show stations on the map as markers
        for (var i = 0; i < stations.length; i++) {
        new google.maps.Marker({
        position: stations[i],
        map: map,
        title: stations[i].name
        });
        }

        // Divide route to several parts because max stations limit is 25 (23 waypoints + 1 origin + 1 destination)
        for (var i = 0, parts = , max = 25 - 1; i < stations.length; i = i + max)
        parts.push(stations.slice(i, i + max + 1));

        // Service callback to process service results
        var service_callback = function(response, status) {
        if (status != 'OK') {
        console.log('Directions request failed due to ' + status);
        return;
        }
        var renderer = new google.maps.DirectionsRenderer;
        renderer.setMap(map);
        renderer.setOptions({ suppressMarkers: true, preserveViewport: true });
        renderer.setDirections(response);
        };

        // Send requests to service to get route (for stations count <= 25 only one request will be sent)
        for (var i = 0; i < parts.length; i++) {
        // Waypoints does not include first station (origin) and last station (destination)
        var waypoints = ;
        for (var j = 1; j < parts[i].length - 1; j++)
        waypoints.push({location: parts[i][j], stopover: false});
        // Service options
        var service_options = {
        origin: parts[i][0],
        destination: parts[i][parts[i].length - 1],
        waypoints: waypoints,
        travelMode: 'WALKING'
        };
        // Send request
        service.route(service_options, service_callback);
        }
        }
        </script>
        <script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"></script>


        You can see screen here.



        fiddle with show/hide line buttons






        function initMap() {
        var service = new google.maps.DirectionsService;
        var map = new google.maps.Map(document.getElementById('map'));

        // list of points
        var stations = [
        {lat: 48.9812840, lng: 21.2171920, name: 'Station 1'},
        {lat: 48.9832841, lng: 21.2176398, name: 'Station 2'},
        {lat: 48.9856443, lng: 21.2209088, name: 'Station 3'},
        {lat: 48.9861461, lng: 21.2261563, name: 'Station 4'},
        {lat: 48.9874682, lng: 21.2294855, name: 'Station 5'},
        {lat: 48.9909244, lng: 21.2295512, name: 'Station 6'},
        {lat: 48.9928871, lng: 21.2292352, name: 'Station 7'},
        {lat: 48.9921334, lng: 21.2246742, name: 'Station 8'},
        {lat: 48.9943196, lng: 21.2234792, name: 'Station 9'},
        {lat: 48.9966345, lng: 21.2221262, name: 'Station 10'},
        {lat: 48.9981191, lng: 21.2271386, name: 'Station 11'},
        {lat: 49.0009168, lng: 21.2359527, name: 'Station 12'},
        {lat: 49.0017950, lng: 21.2392890, name: 'Station 13'},
        {lat: 48.9991912, lng: 21.2398272, name: 'Station 14'},
        {lat: 48.9959850, lng: 21.2418410, name: 'Station 15'},
        {lat: 48.9931772, lng: 21.2453901, name: 'Station 16'},
        {lat: 48.9963512, lng: 21.2525850, name: 'Station 17'},
        {lat: 48.9985134, lng: 21.2508423, name: 'Station 18'},
        {lat: 49.0085000, lng: 21.2508000, name: 'Station 19'},
        {lat: 49.0093000, lng: 21.2528000, name: 'Station 20'},
        {lat: 49.0103000, lng: 21.2560000, name: 'Station 21'},
        {lat: 49.0112000, lng: 21.2590000, name: 'Station 22'},
        {lat: 49.0124000, lng: 21.2620000, name: 'Station 23'},
        {lat: 49.0135000, lng: 21.2650000, name: 'Station 24'},
        {lat: 49.0149000, lng: 21.2680000, name: 'Station 25'},
        {lat: 49.0171000, lng: 21.2710000, name: 'Station 26'},
        {lat: 49.0198000, lng: 21.2740000, name: 'Station 27'},
        {lat: 49.0305000, lng: 21.3000000, name: 'Station 28'},
        ];

        // Zoom and center map automatically by stations (each station will be in visible map area)
        var lngs = stations.map(function(station) { return station.lng; });
        var lats = stations.map(function(station) { return station.lat; });
        map.fitBounds({
        west: Math.min.apply(null, lngs),
        east: Math.max.apply(null, lngs),
        north: Math.min.apply(null, lats),
        south: Math.max.apply(null, lats),
        });

        // Show stations on the map as markers
        for (var i = 0; i < stations.length; i++) {
        if (!stations[i].name)
        continue;
        new google.maps.Marker({
        position: stations[i],
        map: map,
        title: stations[i].name
        });
        }

        // Divide route to several parts because max stations limit is 25 (23 waypoints + 1 origin + 1 destination)
        for (var i = 0, parts = , max = 8 - 1; i < stations.length; i = i + max)
        parts.push(stations.slice(i, i + max + 1));

        // Callback function to process service results
        var service_callback = function(response, status) {
        if (status != 'OK') {
        console.log('Directions request failed due to ' + status);
        return;
        }
        var renderer = new google.maps.DirectionsRenderer;
        renderer.setMap(map);
        renderer.setOptions({ suppressMarkers: true, preserveViewport: true });
        renderer.setDirections(response);
        };

        // Send requests to service to get route (for stations count <= 25 only one request will be sent)
        for (var i = 0; i < parts.length; i++) {
        // Waypoints does not include first station (origin) and last station (destination)
        var waypoints = ;
        for (var j = 1; j < parts[i].length - 1; j++)
        waypoints.push({location: parts[i][j], stopover: false});
        // Service options
        var service_options = {
        origin: parts[i][0],
        destination: parts[i][parts[i].length - 1],
        waypoints: waypoints,
        travelMode: 'WALKING'
        };
        // Send request
        service.route(service_options, service_callback);
        }
        }

        html, body {
        height: 100%;
        margin: 0;
        padding: 0;
        }
        #map {
        height: 100%;
        width: 100%;
        height: 100%;
        }

        <div id="map"></div>

        <!-- without API KEY set variable "max" to 8 -->
        <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script>

        <!-- with API KEY set variable "max" to 25 -->
        <!-- <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap&key=YOUR_API_KEY"></script>-->





        function initMap() {
        var service = new google.maps.DirectionsService;
        var map = new google.maps.Map(document.getElementById('map'));

        // list of points
        var stations = [
        {lat: 48.9812840, lng: 21.2171920, name: 'Station 1'},
        {lat: 48.9832841, lng: 21.2176398, name: 'Station 2'},
        {lat: 48.9856443, lng: 21.2209088, name: 'Station 3'},
        {lat: 48.9861461, lng: 21.2261563, name: 'Station 4'},
        {lat: 48.9874682, lng: 21.2294855, name: 'Station 5'},
        {lat: 48.9909244, lng: 21.2295512, name: 'Station 6'},
        {lat: 48.9928871, lng: 21.2292352, name: 'Station 7'},
        {lat: 48.9921334, lng: 21.2246742, name: 'Station 8'},
        {lat: 48.9943196, lng: 21.2234792, name: 'Station 9'},
        {lat: 48.9966345, lng: 21.2221262, name: 'Station 10'},
        {lat: 48.9981191, lng: 21.2271386, name: 'Station 11'},
        {lat: 49.0009168, lng: 21.2359527, name: 'Station 12'},
        {lat: 49.0017950, lng: 21.2392890, name: 'Station 13'},
        {lat: 48.9991912, lng: 21.2398272, name: 'Station 14'},
        {lat: 48.9959850, lng: 21.2418410, name: 'Station 15'},
        {lat: 48.9931772, lng: 21.2453901, name: 'Station 16'},
        {lat: 48.9963512, lng: 21.2525850, name: 'Station 17'},
        {lat: 48.9985134, lng: 21.2508423, name: 'Station 18'},
        {lat: 49.0085000, lng: 21.2508000, name: 'Station 19'},
        {lat: 49.0093000, lng: 21.2528000, name: 'Station 20'},
        {lat: 49.0103000, lng: 21.2560000, name: 'Station 21'},
        {lat: 49.0112000, lng: 21.2590000, name: 'Station 22'},
        {lat: 49.0124000, lng: 21.2620000, name: 'Station 23'},
        {lat: 49.0135000, lng: 21.2650000, name: 'Station 24'},
        {lat: 49.0149000, lng: 21.2680000, name: 'Station 25'},
        {lat: 49.0171000, lng: 21.2710000, name: 'Station 26'},
        {lat: 49.0198000, lng: 21.2740000, name: 'Station 27'},
        {lat: 49.0305000, lng: 21.3000000, name: 'Station 28'},
        ];

        // Zoom and center map automatically by stations (each station will be in visible map area)
        var lngs = stations.map(function(station) { return station.lng; });
        var lats = stations.map(function(station) { return station.lat; });
        map.fitBounds({
        west: Math.min.apply(null, lngs),
        east: Math.max.apply(null, lngs),
        north: Math.min.apply(null, lats),
        south: Math.max.apply(null, lats),
        });

        // Show stations on the map as markers
        for (var i = 0; i < stations.length; i++) {
        if (!stations[i].name)
        continue;
        new google.maps.Marker({
        position: stations[i],
        map: map,
        title: stations[i].name
        });
        }

        // Divide route to several parts because max stations limit is 25 (23 waypoints + 1 origin + 1 destination)
        for (var i = 0, parts = , max = 8 - 1; i < stations.length; i = i + max)
        parts.push(stations.slice(i, i + max + 1));

        // Callback function to process service results
        var service_callback = function(response, status) {
        if (status != 'OK') {
        console.log('Directions request failed due to ' + status);
        return;
        }
        var renderer = new google.maps.DirectionsRenderer;
        renderer.setMap(map);
        renderer.setOptions({ suppressMarkers: true, preserveViewport: true });
        renderer.setDirections(response);
        };

        // Send requests to service to get route (for stations count <= 25 only one request will be sent)
        for (var i = 0; i < parts.length; i++) {
        // Waypoints does not include first station (origin) and last station (destination)
        var waypoints = ;
        for (var j = 1; j < parts[i].length - 1; j++)
        waypoints.push({location: parts[i][j], stopover: false});
        // Service options
        var service_options = {
        origin: parts[i][0],
        destination: parts[i][parts[i].length - 1],
        waypoints: waypoints,
        travelMode: 'WALKING'
        };
        // Send request
        service.route(service_options, service_callback);
        }
        }

        html, body {
        height: 100%;
        margin: 0;
        padding: 0;
        }
        #map {
        height: 100%;
        width: 100%;
        height: 100%;
        }

        <div id="map"></div>

        <!-- without API KEY set variable "max" to 8 -->
        <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap"></script>

        <!-- with API KEY set variable "max" to 25 -->
        <!-- <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap&key=YOUR_API_KEY"></script>-->






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited yesterday

























        answered Apr 17 '17 at 19:16









        mikep

        570614




        570614








        • 1




          @Tenz store reference to var map and var renderer somewhere to be accessible later. E.g.: window.gMap = map; and if (!window.gRenderers) { window.renderers = ; } window.gRenderers.push(renderer); and later to hide/unhide line use for (var r of gRenderers) r.setMap(null); to hide and for (var r of gRenderers) r.setMap(gMap); to show. Btw to hide line immediately by default add suppressPolylines = true to renderer.setOptions({}). Unfortunatelly setting suppressPolylines later does not work (only on initialization) so we use must use setMap() hack.
          – mikep
          Mar 19 '18 at 9:04
















        • 1




          @Tenz store reference to var map and var renderer somewhere to be accessible later. E.g.: window.gMap = map; and if (!window.gRenderers) { window.renderers = ; } window.gRenderers.push(renderer); and later to hide/unhide line use for (var r of gRenderers) r.setMap(null); to hide and for (var r of gRenderers) r.setMap(gMap); to show. Btw to hide line immediately by default add suppressPolylines = true to renderer.setOptions({}). Unfortunatelly setting suppressPolylines later does not work (only on initialization) so we use must use setMap() hack.
          – mikep
          Mar 19 '18 at 9:04










        1




        1




        @Tenz store reference to var map and var renderer somewhere to be accessible later. E.g.: window.gMap = map; and if (!window.gRenderers) { window.renderers = ; } window.gRenderers.push(renderer); and later to hide/unhide line use for (var r of gRenderers) r.setMap(null); to hide and for (var r of gRenderers) r.setMap(gMap); to show. Btw to hide line immediately by default add suppressPolylines = true to renderer.setOptions({}). Unfortunatelly setting suppressPolylines later does not work (only on initialization) so we use must use setMap() hack.
        – mikep
        Mar 19 '18 at 9:04






        @Tenz store reference to var map and var renderer somewhere to be accessible later. E.g.: window.gMap = map; and if (!window.gRenderers) { window.renderers = ; } window.gRenderers.push(renderer); and later to hide/unhide line use for (var r of gRenderers) r.setMap(null); to hide and for (var r of gRenderers) r.setMap(gMap); to show. Btw to hide line immediately by default add suppressPolylines = true to renderer.setOptions({}). Unfortunatelly setting suppressPolylines later does not work (only on initialization) so we use must use setMap() hack.
        – mikep
        Mar 19 '18 at 9:04













        0














        Here is an hack to use more than 8 waypoints.Please checkout my solution for that.



        Drawing roadmap with more than 8 waypoints using Google directions API






        share|improve this answer




























          0














          Here is an hack to use more than 8 waypoints.Please checkout my solution for that.



          Drawing roadmap with more than 8 waypoints using Google directions API






          share|improve this answer


























            0












            0








            0






            Here is an hack to use more than 8 waypoints.Please checkout my solution for that.



            Drawing roadmap with more than 8 waypoints using Google directions API






            share|improve this answer














            Here is an hack to use more than 8 waypoints.Please checkout my solution for that.



            Drawing roadmap with more than 8 waypoints using Google directions API







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 23 '17 at 11:54









            Community

            11




            11










            answered Aug 16 '15 at 19:11









            Nullify

            9,53572653




            9,53572653























                0














                There is a easy around this solution.



                Keep the waypoints in array based on the distance threshold value and keep adding them. Once you reach the limit of 8 values in the array, assign the 1st position(Origin) of the waypoint array to a new waypoint array .... Assign the last waypoint to the new waypoint as the 2nd element... now replace the old waypoint array with this new one and continue.



                Watever you do the waypoints will never cross more than 8 values and it will keep track of the route taken to map it out(Unless the journey is way too long)



                var addWaypoint = function(point) {
                if($scope.waypoints.length > 8){
                var temp = ;
                temp.push($scope.waypoints[0]); //Start Point
                temp.push($scope.waypoints[7]); //Last point
                temp.push(point); //New point
                $scope.waypoints = temp; //Replace the old object with this new one
                }
                else
                $scope.waypoints.push(point);
                }





                share|improve this answer


























                  0














                  There is a easy around this solution.



                  Keep the waypoints in array based on the distance threshold value and keep adding them. Once you reach the limit of 8 values in the array, assign the 1st position(Origin) of the waypoint array to a new waypoint array .... Assign the last waypoint to the new waypoint as the 2nd element... now replace the old waypoint array with this new one and continue.



                  Watever you do the waypoints will never cross more than 8 values and it will keep track of the route taken to map it out(Unless the journey is way too long)



                  var addWaypoint = function(point) {
                  if($scope.waypoints.length > 8){
                  var temp = ;
                  temp.push($scope.waypoints[0]); //Start Point
                  temp.push($scope.waypoints[7]); //Last point
                  temp.push(point); //New point
                  $scope.waypoints = temp; //Replace the old object with this new one
                  }
                  else
                  $scope.waypoints.push(point);
                  }





                  share|improve this answer
























                    0












                    0








                    0






                    There is a easy around this solution.



                    Keep the waypoints in array based on the distance threshold value and keep adding them. Once you reach the limit of 8 values in the array, assign the 1st position(Origin) of the waypoint array to a new waypoint array .... Assign the last waypoint to the new waypoint as the 2nd element... now replace the old waypoint array with this new one and continue.



                    Watever you do the waypoints will never cross more than 8 values and it will keep track of the route taken to map it out(Unless the journey is way too long)



                    var addWaypoint = function(point) {
                    if($scope.waypoints.length > 8){
                    var temp = ;
                    temp.push($scope.waypoints[0]); //Start Point
                    temp.push($scope.waypoints[7]); //Last point
                    temp.push(point); //New point
                    $scope.waypoints = temp; //Replace the old object with this new one
                    }
                    else
                    $scope.waypoints.push(point);
                    }





                    share|improve this answer












                    There is a easy around this solution.



                    Keep the waypoints in array based on the distance threshold value and keep adding them. Once you reach the limit of 8 values in the array, assign the 1st position(Origin) of the waypoint array to a new waypoint array .... Assign the last waypoint to the new waypoint as the 2nd element... now replace the old waypoint array with this new one and continue.



                    Watever you do the waypoints will never cross more than 8 values and it will keep track of the route taken to map it out(Unless the journey is way too long)



                    var addWaypoint = function(point) {
                    if($scope.waypoints.length > 8){
                    var temp = ;
                    temp.push($scope.waypoints[0]); //Start Point
                    temp.push($scope.waypoints[7]); //Last point
                    temp.push(point); //New point
                    $scope.waypoints = temp; //Replace the old object with this new one
                    }
                    else
                    $scope.waypoints.push(point);
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Oct 4 '16 at 13:20









                    anupsahu

                    238




                    238























                        -2














                        The code below in C# calculates how many calls you will make Google Directions API and how many waypoints on each iteration. You can modify the Modmin to change the minimum waypoints that you want on your last iteration.



                        For example if you have totalWaypoints.Count = 97:



                        97 Mod 23 = 5, in this case i want a Modmin greater than 5, so i will calculate again with a lower waypointsByIteration;



                        97 Mod 22 = 9, (9 > Modmin) , OK;



                        iterations = ((97 - (97 % 22)) / (22)) + 1 = 5;



                        On the last iteration waypointsByIteration will be the residue.



                                var iterations = 1;//Number of iterations
                        var waypointsByIteration = 23;//Number of waypoints by iteration
                        var modMin = 5;//Minimum of Waypoints in the last iteration
                        var residue = 0;//Residue of the division (totalWaypoints % waypointsByIteration)

                        if (totalWaypoints.Count <= waypointsByIteration)
                        {
                        waypointsByIteration = totalWaypoints.Count;
                        }
                        else
                        {
                        while (totalWaypoints.Count % waypointsByIteration < modMin)
                        {
                        waypointsByIteration--;
                        }

                        //Calculate number of waypoints by iteracoes
                        iterations = ((totalWaypoints.Count - (totalWaypoints.Count % waypointsByIteration)) / (waypointsByIteration)) + 1;
                        }

                        residue = totalWaypoints % waypointsByIteration;

                        for(i=0;i<iterations;i++)
                        {
                        //If it is the last index, the waypointsByIteration will be the residue
                        if(i == iteration - 1)
                        {
                        waypointsByIteration = residue;
                        }
                        }





                        share|improve this answer


























                          -2














                          The code below in C# calculates how many calls you will make Google Directions API and how many waypoints on each iteration. You can modify the Modmin to change the minimum waypoints that you want on your last iteration.



                          For example if you have totalWaypoints.Count = 97:



                          97 Mod 23 = 5, in this case i want a Modmin greater than 5, so i will calculate again with a lower waypointsByIteration;



                          97 Mod 22 = 9, (9 > Modmin) , OK;



                          iterations = ((97 - (97 % 22)) / (22)) + 1 = 5;



                          On the last iteration waypointsByIteration will be the residue.



                                  var iterations = 1;//Number of iterations
                          var waypointsByIteration = 23;//Number of waypoints by iteration
                          var modMin = 5;//Minimum of Waypoints in the last iteration
                          var residue = 0;//Residue of the division (totalWaypoints % waypointsByIteration)

                          if (totalWaypoints.Count <= waypointsByIteration)
                          {
                          waypointsByIteration = totalWaypoints.Count;
                          }
                          else
                          {
                          while (totalWaypoints.Count % waypointsByIteration < modMin)
                          {
                          waypointsByIteration--;
                          }

                          //Calculate number of waypoints by iteracoes
                          iterations = ((totalWaypoints.Count - (totalWaypoints.Count % waypointsByIteration)) / (waypointsByIteration)) + 1;
                          }

                          residue = totalWaypoints % waypointsByIteration;

                          for(i=0;i<iterations;i++)
                          {
                          //If it is the last index, the waypointsByIteration will be the residue
                          if(i == iteration - 1)
                          {
                          waypointsByIteration = residue;
                          }
                          }





                          share|improve this answer
























                            -2












                            -2








                            -2






                            The code below in C# calculates how many calls you will make Google Directions API and how many waypoints on each iteration. You can modify the Modmin to change the minimum waypoints that you want on your last iteration.



                            For example if you have totalWaypoints.Count = 97:



                            97 Mod 23 = 5, in this case i want a Modmin greater than 5, so i will calculate again with a lower waypointsByIteration;



                            97 Mod 22 = 9, (9 > Modmin) , OK;



                            iterations = ((97 - (97 % 22)) / (22)) + 1 = 5;



                            On the last iteration waypointsByIteration will be the residue.



                                    var iterations = 1;//Number of iterations
                            var waypointsByIteration = 23;//Number of waypoints by iteration
                            var modMin = 5;//Minimum of Waypoints in the last iteration
                            var residue = 0;//Residue of the division (totalWaypoints % waypointsByIteration)

                            if (totalWaypoints.Count <= waypointsByIteration)
                            {
                            waypointsByIteration = totalWaypoints.Count;
                            }
                            else
                            {
                            while (totalWaypoints.Count % waypointsByIteration < modMin)
                            {
                            waypointsByIteration--;
                            }

                            //Calculate number of waypoints by iteracoes
                            iterations = ((totalWaypoints.Count - (totalWaypoints.Count % waypointsByIteration)) / (waypointsByIteration)) + 1;
                            }

                            residue = totalWaypoints % waypointsByIteration;

                            for(i=0;i<iterations;i++)
                            {
                            //If it is the last index, the waypointsByIteration will be the residue
                            if(i == iteration - 1)
                            {
                            waypointsByIteration = residue;
                            }
                            }





                            share|improve this answer












                            The code below in C# calculates how many calls you will make Google Directions API and how many waypoints on each iteration. You can modify the Modmin to change the minimum waypoints that you want on your last iteration.



                            For example if you have totalWaypoints.Count = 97:



                            97 Mod 23 = 5, in this case i want a Modmin greater than 5, so i will calculate again with a lower waypointsByIteration;



                            97 Mod 22 = 9, (9 > Modmin) , OK;



                            iterations = ((97 - (97 % 22)) / (22)) + 1 = 5;



                            On the last iteration waypointsByIteration will be the residue.



                                    var iterations = 1;//Number of iterations
                            var waypointsByIteration = 23;//Number of waypoints by iteration
                            var modMin = 5;//Minimum of Waypoints in the last iteration
                            var residue = 0;//Residue of the division (totalWaypoints % waypointsByIteration)

                            if (totalWaypoints.Count <= waypointsByIteration)
                            {
                            waypointsByIteration = totalWaypoints.Count;
                            }
                            else
                            {
                            while (totalWaypoints.Count % waypointsByIteration < modMin)
                            {
                            waypointsByIteration--;
                            }

                            //Calculate number of waypoints by iteracoes
                            iterations = ((totalWaypoints.Count - (totalWaypoints.Count % waypointsByIteration)) / (waypointsByIteration)) + 1;
                            }

                            residue = totalWaypoints % waypointsByIteration;

                            for(i=0;i<iterations;i++)
                            {
                            //If it is the last index, the waypointsByIteration will be the residue
                            if(i == iteration - 1)
                            {
                            waypointsByIteration = residue;
                            }
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered May 4 '17 at 14:10









                            Jonathan Molina

                            518412




                            518412

















                                protected by Community Sep 19 '17 at 14:06



                                Thank you for your interest in this question.
                                Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                Would you like to answer one of these unanswered questions instead?



                                Popular posts from this blog

                                "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                                Alcedinidae

                                Origin of the phrase “under your belt”?