banner



How To Draw A Map On Google Maps

  • Updated date April 13, 2020
  • 518k
  • 24

In this article you will learn how to draw and work with routes on the fly using the Google Maps API V3.

Introduction

This article volition aid you to solve the following problems:

  1. How to describe a route on the wing.
  2. How to delete a location from a route on the fly.
  3. How to swap the routes on Google Maps using an HTML table.
  4. How to summate route distance and time with respect to speed.

I have used waypoints to draw the routes. Please note that you can use a maximum of x locations at a time. In this article, I have made all necessary JavaScript code comments in the "googlemap.js" JavaScript file. So I am not explaining the JavaScript lawmaking. Please download the attachment for more details about code.

Procedure

Use the post-obit procedure to draw a route on the fly:

  1. Run the projection.
  2. Double-click on the start location on the Google map.

    MapAVI1.jpg

    Here I have chosen Mumbai equally the starting location.
  3. Yous can choose the second location by double-clicking on another location on the map or drag the "B" icon to the second location.

    MapAVI2.jpg

    Here I take called Pune as my second location.
  4. You can choose some other location by double-clicking on the map.

    MapAVI3.jpg

    Hither I take chosen Hyderabad equally my third location.
  5. Notice that when y'all click on the map, the table will automatically generate the latitude, longitude, distance and time.
  6. Toe calculate the fourth dimension, you lot demand to enter the speed in the TextBox.

Lawmaking

1. Initialize the map on folio load

  1. var  directionsService = new  google.maps.DirectionsService();
  2. var  _mapPoints = new  Array();
  3. var  _directionsRenderer = '' ;
  4. function  InitializeMap() {
  5.     _directionsRenderer =new  google.maps.DirectionsRenderer();
  6. var  myOptions = {
  7.         zoom: 6,
  8.         center:new  google.maps.LatLng(21.7679, 78.8718),
  9.         mapTypeId: google.maps.MapTypeId.ROADMAP
  10.     };
  11. var  map = new  google.maps.Map(document.getElementById( "dvMap" ), myOptions);
  12.     _directionsRenderer.setMap(map);
  13.     _directionsRenderer.setOptions({
  14.         draggable:truthful
  15.     });
  16.     google.maps.event.addListener(map,"dblclick" , function  (outcome) {
  17. if  ($( "#txtAvgSpeed" ).val() == '' ) {
  18.             alert("Please enter the Average Speed (km/60 minutes)." );
  19.             $("#txtAvgSpeed" ).focus();
  20. return false ;
  21.         }
  22. var  _currentPoints = event.latLng;
  23.         _mapPoints.push button(_currentPoints);
  24.         getRoutePointsAndWaypoints();
  25.     });
  26.     google.maps.event.addListener(_directionsRenderer,'directions_changed' , function  () {
  27.         computeTotalDistanceforRoute(_directionsRenderer.directions);
  28.     });
  29. }

2. Get the route points and waypoints

  1. function  getRoutePointsAndWaypoints() {
  2. var  _waypoints = new  Array();
  3. if  (_mapPoints.length > 2)
  4.     {
  5. for  ( var  j = 1; j < _mapPoints.length - 1; j++) {
  6. var  address = _mapPoints[j];
  7. if  (address !== "" ) {
  8.                 _waypoints.button({
  9.                     location: address,
  10.                     stopover:true
  11.                 });
  12.             }
  13.         }
  14.         drawRoute(_mapPoints[0], _mapPoints[_mapPoints.length - ane], _waypoints);
  15.     }else if  (_mapPoints.length > 1) {
  16.         drawRoute(_mapPoints[_mapPoints.length - 2], _mapPoints[_mapPoints.length - i], _waypoints);
  17.     }else  {
  18.         drawRoute(_mapPoints[_mapPoints.length - 1], _mapPoints[_mapPoints.length - 1], _waypoints);
  19.     }
  20. }

3. Draw the route

The following function is used to describe 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.road(_request,function  (_response, _status) {
  19. if  (_status == google.maps.DirectionsStatus.OK) {
  20.                 _directionsRenderer.setDirections(_response);
  21.             }
  22.         });
  23.     }

How to delete a location from the route on the fly

  1. If I desire to delete the Pune location grade the instance in a higher place then I click on the "Ten" image button to delete the location.

    MapAVI4.jpg

  2. When you click on the "Ok" push button, the "Pune" or "B" location will be deleted.

    MapAVI5.jpg

Code

ane. The following is the lawmaking to delete the required location:

  1. function  deleteLocation(trid) {
  2. if  (ostend( "Are you certain want to delete this location?" ) == true ) {
  3. var  _temPoint = new  Assortment();
  4. for  ( var  w = 0; westward < _mapPoints.length; due west++) {
  5. if  (trid != w + 1) {
  6.                     _temPoint.push(_mapPoints[due west]);
  7.                 }
  8.             }
  9.             _mapPoints =new  Array();
  10. for  ( var  y = 0; y < _temPoint.length; y++) {
  11.                 _mapPoints.push button(_temPoint[y]);
  12.             }
  13.             getRoutePointsAndWaypoints();
  14.         }else  {
  15. return false ;
  16.         }
  17.     }

2. I take called the deleteLocation() method on image click on click event.

  1. htmlhtml = html + "<td manner=\"width: 60px;\"><img alt=\"DeleteLocation\" src=\"Images/Delete.jpg\" onclick=\"return deleteLocation("  + _htmlTrCount + ");\" /></td>" ;

How to swap the routes on Google map using HTML table

1. In the case above, I have created iii locations, they are Bombay, Pune, and Hyderabad.

MapAVI6.jpg

2. Now I desire to swap the locations, in other words, my start locations will be Hyderabad, then Pune, so Mumbai.

3. Put the mouse on the third row of the table or the "Location Name: C" tabular array row then drag and driblet to the offset row, in other words the offset row or "Location Name: A".

MapAVI7.jpg

iv. So my start location is "A", in other words, Hyderabad. Now the 2nd location volition exist Pune. Right at present it is showing Mumbai. Do the aforementioned for Bombay.

MapAVI8.jpg

Code

ane. The post-obit code volition help yous to move the locations from the HTML table:

  1. part  draganddrophtmltablerows() {
  2. var  _tempPoints = new  Array();
  3.         $("#HtmlTable" ).tableDnD();
  4.         $("#HtmlTable" ).tableDnD({
  5.             onDrop:function  (tabular array, row) {
  6. var  rows = tabular array.tBodies[0].rows;
  7. for  ( var  q = 0; q < rows.length; q++) {
  8.                     _tempPoints.push button(_mapPoints[rows[q].id - 1]);
  9.                 }
  10.                 _mapPoints =new  Assortment();
  11. for  ( var  y = 0; y < _tempPoints.length; y++) {
  12.                     _mapPoints.push(_tempPoints[y]);
  13.                 }
  14.                 getRoutePointsAndWaypoints();
  15.             }
  16.         });

2. I have used the Scripts/jquery.tablednd.js for this table swap.

Calculate route altitude and time with respect to speed

The following process will calculate the road distance and time with respect to speed.

one. When you create a location on the map, the distance and speed will automatically exist calculated.

MapAVI9.jpg

ii. For speed, you need to enter the "Boilerplate Speed (km/hr)" in the text box.

Code

  1. function  CreateHTMTable(_latlng, _distance) {
  2. var  _Speed = $( "#txtAvgSpeed" ).val();
  3. var  _Time = parseInt(((_distance / 1000) / _Speed) * lx);;
  4. if  (_htmlTrCount - ane == 0) {
  5.             _Time = 0;
  6.             _distance = 0;
  7.         }
  8. var  html = '' ;
  9. var  championship = new  Assortment( "A" , "B" , "C" , "D" , "E" , "F" , "G" , "H" , "I" , "J" , "1000" , "L" , "1000" , "Due north" , "O" );
  10.         html = html +"<tr id=\""  + _htmlTrCount + "\">" ;
  11.         html = html +"<td manner=\"width: 80px;\">"  + _htmlTrCount + "</td>" ;
  12.         html = html +"<td style=\"width: 80px;\"><bridge id=\"Title_"  + _htmlTrCount + "\">"  + title[_htmlTrCount - 1] + "</span></td>" ;
  13.         html = html +"<td manner=\"width: 100px;\"><span id=\"lat_"  + _htmlTrCount + "\">"  + parent.Cord(_latlng).split up( "," )[0].substring(one, 8) + "</span></td>" ;
  14.         html = html +"<td style=\"width: 100px;\"><span id=\"lng_"  + _htmlTrCount + "\">"  + parent.Cord(_latlng).split( "," )[1].substring(1, 8) + "</span></td>" ;
  15.         html = html +"<td mode=\"width: 100px;\"><span id=\"dir_"  + _htmlTrCount + "\">"  + _distance + "</span></td>" ;
  16.         html = html +"<td style=\"width: 70px;\"><bridge id=\"time_"  + _htmlTrCount + "\">"  + _Time + "</span></td>" ;
  17.         html = html +"<td style=\"width: 60px;\"><img alt=\"DeleteLocation\" src=\"Images/Delete.jpg\" onclick=\"render deleteLocation("  + _htmlTrCount + ");\" /></td>" ;
  18.         html = html +"</tr>" ;
  19.         $("#HtmlTable" ).append(html);
  20.         draganddrophtmltablerows();
  21.     }

The post-obit lawmaking volition help you to calculate distance and speed.

NOTE:

  1. Equally I already mentioned at the top of this article, I have used the waypoints to plot the route. So you can use a maximum of 10 waypoints with the gratis ane.
  2. To better sympathise, download the source code in the attachment and run it to exam it.
  3. I have used the Google map API v3 for this article.
  4. Please comment on this article for better comeback and I promise y'all bask the article.

Source: https://www.c-sharpcorner.com/UploadFile/8911c4/how-to-draw-routes-and-calculate-route-time-and-distance-on/

Posted by: harrisonsiquene.blogspot.com

0 Response to "How To Draw A Map On Google Maps"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel