Introduction

Everyone knows that we can choose 10 locations and draw a route among these locations on a Google Map using API V3. This is a Google Maps limitation. But today I will show how to draw an infinite route with more than 10 locations on a Google Map with API V3.
This article will help you to solve the following problems:
  1. How to draw a route on the fly.
  2. How to draw an infinite route with more than 10 locations.
See the following image where I have drawn the infinite route with more than 10 locations on it.
Google Map
In this picture, the red route has more than 10 points for the route. Here there are 14 locations, an infinite route.
The following is another example:
Google Map1
JavaScript Code
1. Initialize the map on page load
//You can calculate directions (using a variety of methods of transportation) by using the DirectionsService object.
var directionsService = new google.maps.DirectionsService();
//Define a variable with all map points.
var _mapPoints = new Array();
//Define a DirectionsRenderer variable.
var _directionsRenderer = '';
//This will give you the map zoom value.
var zoom_option = 6;
//LegPoints is your route points between two locations.
var LegPoints = new Array();
//Google map object
var map;
//InitializeMap() function is used to initialize google map on page load.
function InitializeMap() {
//DirectionsRenderer() is a used to render the direction
_directionsRenderer = new google.maps.DirectionsRenderer();
  1. //Set the your own options for map.
  2. var myOptions = {
  3. zoom: zoom_option,
  4. zoomControl: true,
  5. center: new google.maps.LatLng(21.7679, 78.8718),
  6. mapTypeId: google.maps.MapTypeId.ROADMAP
  7. };
  8. //Define the map.
  9. map = new google.maps.Map(document.getElementById("dvMap"), myOptions);
  10. //Set the map for directionsRenderer
  11. _directionsRenderer.setMap(map);
  12. //Set different options for DirectionsRenderer mehtods.
  13. //draggable option will used to drag the route.
  14. _directionsRenderer.setOptions({
  15. draggable: true
  16. });
  17. //Add the doubel click event to map.
  18. google.maps.event.addListener(map, "dblclick", function(event) {
  19. var _currentPoints = event.latLng;
  20. _mapPoints.push(_currentPoints);
  21. LegPoints.push('');
  22. getRoutePointsAndWaypoints(_mapPoints);
  23. });
  24. //Add the directions changed event to map.
  25. google.maps.event.addListener(_directionsRenderer, 'directions_changed', function() {
  26. var myroute = _directionsRenderer.directions.routes[0];
  27. CreateRoute(myroute);
  28. zoom_option = map.getZoom();
  29. });
  30. }
2. Get the route points and waypoints
  1. //getRoutePointsAndWaypoints() will help you to pass points and waypoints to drawRoute() function
  2. function getRoutePointsAndWaypoints(Points) {
  3. if (Points.length <= 10) {
  4. drawRoutePointsAndWaypoints(Points);
  5. }
  6. else {
  7. var newPoints = new Array();
  8. var startPoint = Points.length - 10;
  9. var Legs = Points.length - 10;
  10. for (var i = startPoint; i < Points.length; i++) {
  11. newPoints.push(Points[i]);
  12. }
  13. drawRoutePointsAndWaypoints(newPoints);
  14. drawPreviousRoute(Legs);
  15. }
  16. }
  17. function drawRoutePointsAndWaypoints(Points) {
  18. //Define a variable for waypoints.
  19. var _waypoints = new Array();
  20. if (Points.length > 2) //Waypoints will be come.
  21. {
  22. for (var j = 1; j < Points.length - 1; j++) {
  23. var address = Points[j];
  24. if (address !== "") {
  25. _waypoints.push({
  26. location: address,
  27. stopover: true //stopover is used to show marker on map for waypoints
  28. });
  29. }
  30. }
  31. //Call a drawRoute() function
  32. drawRoute(Points[0], Points[Points.length - 1], _waypoints);
  33. } else if (Points.length > 1) {
  34. //Call a drawRoute() function only for start and end locations
  35. drawRoute(Points[_mapPoints.length - 2], Points[Points.length - 1], _waypoints);
  36. } else {
  37. //Call a drawRoute() function only for one point as start and end locations.
  38. drawRoute(Points[_mapPoints.length - 1], Points[Points.length - 1], _waypoints);
  39. }
  40. }
3. Draw the route
  1. //drawRoute() will help actual draw the route on map.
  2. function drawRoute(originAddress, destinationAddress, _waypoints) {
  3. //Define a request variable for route .
  4. var _request = '';
  5. //This is for more then two locatins
  6. if (_waypoints.length > 0) {
  7. _request = {
  8. origin: originAddress,
  9. destination: destinationAddress,
  10. waypoints: _waypoints, //an array of waypoints
  11. optimizeWaypoints: true, //set to true if you want google to determine the shortest route or false to use the order specified.
  12. travelMode: google.maps.DirectionsTravelMode.DRIVING
  13. };
  14. } else {
  15. //This is for one or two locations. Here noway point is used.
  16. _request = {
  17. origin: originAddress,
  18. destination: destinationAddress,
  19. travelMode: google.maps.DirectionsTravelMode.DRIVING
  20. };
  21. }
  22. //This will take the request and draw the route and return response and status as output
  23. directionsService.route(_request, function(_response, _status) {
  24. if (_status == google.maps.DirectionsStatus.OK) {
  25. _directionsRenderer.setDirections(_response);
  26. }
  27. });
  28. }
Notes:
  1. I have used waypoints to draw the routes.
  2. In this article, I have made all necessary JavaScript code comments in the "googlemap.js" JavaScript file.
  3. So I am not explaining the JavaScript code.
  4. Please download the attachment for more details about the code.
  5. I have used the Google Map API v3 for this article.
Please comment on this article for better improvement and I hope you enjoy the article.