The following is my aspx:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>Search Route Direction</title>
  6. <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
  7. <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC6v5-2uaq_wusHDktM9ILcqIrlPtnZgEk&sensor=false">
  8. </script>
  9. <%--Getting User Current Location--%>
  10. <script type="text/javascript">
  11. if (navigator.geolocation) {
  12. navigator.geolocation.getCurrentPosition(success);
  13. } else {
  14. alert("There is Some Problem on your current browser to get Geo Location!");
  15. }
  16. function success(position) {
  17. var lat = position.coords.latitude;
  18. var long = position.coords.longitude;
  19. var city = position.coords.locality;
  20. var LatLng = new google.maps.LatLng(lat, long);
  21. var mapOptions = {
  22. center: LatLng,
  23. zoom: 12,
  24. mapTypeId: google.maps.MapTypeId.ROADMAP
  25. };
  26. var map = new google.maps.Map(document.getElementById("MyMapLOC"), mapOptions);
  27. var marker = new google.maps.Marker({
  28. position: LatLng,
  29. title: "<div style = 'height:60px;width:200px'><b>Your location:</b><br />Latitude: "
  30. + lat + +"<br />Longitude: " + long
  31. });
  32. marker.setMap(map);
  33. var getInfoWindow = new google.maps.InfoWindow({ content: "<b>Your Current Location</b><br/> Latitude:" +
  34. lat + "<br /> Longitude:" + long + ""
  35. });
  36. getInfoWindow.open(map, marker);
  37. }
  38. </script>
  39. <%--Getting Route Direction From User Current Location to Destination--%>
  40. <script type="text/javascript">
  41. function SearchRoute() {
  42. document.getElementById("MyMapLOC").style.display = 'none';
  43. var markers = new Array();
  44. var myLatLng;
  45. //Find the current location of the user.
  46. if (navigator.geolocation) {
  47. navigator.geolocation.getCurrentPosition(function(p) {
  48. var myLatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude);
  49. var m = {};
  50. m.title = "Your Current Location";
  51. m.lat = p.coords.latitude;
  52. m.lng = p.coords.longitude;
  53. markers.push(m);
  54. //Find Destination address location.
  55. var address = document.getElementById("txtDestination").value;
  56. var geocoder = new google.maps.Geocoder();
  57. geocoder.geocode({ 'address': address }, function(results, status) {
  58. if (status == google.maps.GeocoderStatus.OK) {
  59. m = {};
  60. m.title = address;
  61. m.lat = results[0].geometry.location.lat();
  62. m.lng = results[0].geometry.location.lng();
  63. markers.push(m);
  64. var mapOptions = {
  65. center: myLatLng,
  66. zoom: 4,
  67. mapTypeId: google.maps.MapTypeId.ROADMAP
  68. };
  69. var map = new google.maps.Map(document.getElementById("MapRoute"), mapOptions);
  70. var infoWindow = new google.maps.InfoWindow();
  71. var lat_lng = new Array();
  72. var latlngbounds = new google.maps.LatLngBounds();
  73. for (i = 0; i < markers.length; i++) {
  74. var data = markers[i];
  75. var myLatlng = new google.maps.LatLng(data.lat, data.lng);
  76. lat_lng.push(myLatlng);
  77. var marker = new google.maps.Marker({
  78. position: myLatlng,
  79. map: map,
  80. title: data.title
  81. });
  82. latlngbounds.extend(marker.position);
  83. (function(marker, data) {
  84. google.maps.event.addListener(marker, "click", function(e) {
  85. infoWindow.setContent(data.title);
  86. infoWindow.open(map, marker);
  87. });
  88. })(marker, data);
  89. }
  90. map.setCenter(latlngbounds.getCenter());
  91. map.fitBounds(latlngbounds);
  92. //***********ROUTING****************//
  93. //Initialize the Path Array.
  94. var path = new google.maps.MVCArray();
  95. //Getting the Direction Service.
  96. var service = new google.maps.DirectionsService();
  97. //Set the Path Stroke Color.
  98. var poly = new google.maps.Polyline({ map: map, strokeColor: '#4986E7' });
  99. //Loop and Draw Path Route between the Points on MAP.
  100. for (var i = 0; i < lat_lng.length; i++) {
  101. if ((i + 1) < lat_lng.length) {
  102. var src = lat_lng[i];
  103. var des = lat_lng[i + 1];
  104. path.push(src);
  105. poly.setPath(path);
  106. service.route({
  107. origin: src,
  108. destination: des,
  109. travelMode: google.maps.DirectionsTravelMode.DRIVING
  110. }, function(result, status) {
  111. if (status == google.maps.DirectionsStatus.OK) {
  112. for (var i = 0, len = result.routes[0].overview_path.length; i < len; i++) {
  113. path.push(result.routes[0].overview_path[i]);
  114. }
  115. } else {
  116. alert("Invalid location.");
  117. window.location.href = window.location.href;
  118. }
  119. });
  120. }
  121. }
  122. } else {
  123. alert("Request failed.")
  124. }
  125. });
  126. });
  127. }
  128. else {
  129. alert('Some Problem in getting Geo Location.');
  130. return;
  131. }
  132. }
  133. </script>
  134. </head>
  135. <body>
  136. <form id="form1" runat="server">
  137. <table style="border: solid 15px blue; width: 100%; vertical-align: central;">
  138. <tr>
  139. <td style="padding-left: 20px; padding-top: 20px; padding-bottom: 20px; background-color: skyblue;
  140. text-align: center; font-family: Verdana; font-size: 20pt; color: Green;">
  141. Draw Route Between User's Current Location & Destination On Google Map
  142. </td>
  143. </tr>
  144. <tr>
  145. <td style="background-color: skyblue; text-align: center; font-family: Verdana; font-size: 14pt;
  146. color: red;">
  147. <b>Enter Destination:</b>
  148. <input type="text" id="txtDestination" value="" style="width: 200px" />
  149. <input type="button" value="Search Route" onclick="SearchRoute()" />
  150. </td>
  151. </tr>
  152. <tr>
  153. <td>
  154. <div id="MyMapLOC" style="width: 550px; height: 470px">
  155. </div>
  156. <div id="MapRoute" style="width: 500px; height: 500px">
  157. </div>
  158. </td>
  159. </tr>
  160. </form>
  161. </body>
  162. </html>

Now run the application.

current location

Enter your destination to draw a route.

 enter destination

agra destination

mumbai destination