Javascript required
Skip to content Skip to sidebar Skip to footer

Google Maps Drawing Mutiple Areas

  • Updated date Apr 16, 2020
  • 317k
  • 6

Google Maps are limited to 10 locations but this article shows how to draw a route with more than 10 locations on Google Maps with API V3.

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. var  myOptions = {
  2.         zoom: zoom_option,
  3.         zoomControl:true ,
  4.         center:new  google.maps.LatLng(21.7679, 78.8718),
  5.         mapTypeId: google.maps.MapTypeId.ROADMAP
  6.     };
  7.     map =new  google.maps.Map(document.getElementById( "dvMap" ), myOptions);
  8.     _directionsRenderer.setMap(map);
  9.     _directionsRenderer.setOptions({
  10.         draggable:true
  11.     });
  12.     google.maps.event.addListener(map,"dblclick" , function (event) {
  13. var  _currentPoints = event.latLng;
  14.         _mapPoints.push(_currentPoints);
  15.         LegPoints.push('' );
  16.         getRoutePointsAndWaypoints(_mapPoints);
  17.     });
  18.     google.maps.event.addListener(_directionsRenderer,'directions_changed' , function () {
  19. var  myroute = _directionsRenderer.directions.routes[0];
  20.         CreateRoute(myroute);
  21.         zoom_option = map.getZoom();
  22.     });
  23. }

2. Get the route points and waypoints

  1. function  getRoutePointsAndWaypoints(Points) {
  2. if  (Points.length <= 10) {
  3.         drawRoutePointsAndWaypoints(Points);
  4.     }
  5. else  {
  6. var  newPoints = new  Array();
  7. var  startPoint = Points.length - 10;
  8. var  Legs = Points.length - 10;
  9. for  ( var  i = startPoint; i < Points.length; i++) {
  10.             newPoints.push(Points[i]);
  11.         }
  12.         drawRoutePointsAndWaypoints(newPoints);
  13.         drawPreviousRoute(Legs);
  14.     }
  15. }
  16. function  drawRoutePointsAndWaypoints(Points) {
  17. var  _waypoints = new  Array();
  18. if  (Points.length > 2)
  19.     {
  20. for  ( var  j = 1; j < Points.length - 1; j++) {
  21. var  address = Points[j];
  22. if  (address !== "" ) {
  23.                 _waypoints.push({
  24.                     location: address,
  25.                     stopover:true
  26.                 });
  27.             }
  28.         }
  29.         drawRoute(Points[0], Points[Points.length - 1], _waypoints);
  30.     }else if  (Points.length > 1) {
  31.         drawRoute(Points[_mapPoints.length - 2], Points[Points.length - 1], _waypoints);
  32.     }else  {
  33.         drawRoute(Points[_mapPoints.length - 1], Points[Points.length - 1], _waypoints);
  34.     }
  35. }

3. Draw the route

  1. function  drawRoute(originAddress, destinationAddress, _waypoints) {
  2. var  _request = '' ;
  3. if  (_waypoints.length > 0) {
  4.         _request = {
  5.             origin: originAddress,
  6.             destination: destinationAddress,
  7.             waypoints: _waypoints,
  8.             optimizeWaypoints:true ,
  9.             travelMode: google.maps.DirectionsTravelMode.DRIVING
  10.         };
  11.     }else  {
  12.         _request = {
  13.             origin: originAddress,
  14.             destination: destinationAddress,
  15.             travelMode: google.maps.DirectionsTravelMode.DRIVING
  16.         };
  17.     }
  18.     directionsService.route(_request,function (_response, _status) {
  19. if  (_status == google.maps.DirectionsStatus.OK) {
  20.             _directionsRenderer.setDirections(_response);
  21.         }
  22.     });
  23. }

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.

Google Maps Drawing Mutiple Areas

Source: https://www.c-sharpcorner.com/UploadFile/8911c4/how-to-draw-infinite-route-with-more-than-10-locations-on-go/