How to implement Google Mapping with Get Direction and distance from one location to another location in using JavaScript || Google Map Searching with Get Direction in Web Application using JavaScript with GoogleAPI.

The following  article is shows about how to use Google maps with direction and distance in application.
For that first I am taking two textboxes to enter From location and for To location ,one GetDirection button and one division to display map.
The following steps show the complete information:
1.       Open Visual StudioàFileàNewàASP.Net Empty WebsiteàGive the name as GooglemappingwithDirectionàOK
2.       Add one webpage to the solutions and give the name as GooglemapsDirection.aspx
3.       Design the form like the following:

4.       Write the following code in GooglemapsDirection.aspx source page using JavaScript.

   <form id="form1" runat="server">
    <script type="text/javascript" src=""></script>
<script language="javascript" type="text/javascript">
    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();

    function InitializeMap() {
        directionsDisplay = new google.maps.DirectionsRenderer();
        var latlng = new google.maps.LatLng(17.425503, 78.47497);
        var myOptions =
            zoom: 13,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        var map = new google.maps.Map(document.getElementById("Gmap"), myOptions);


        var control = document.getElementById('control'); = 'block';


    function calcRoute() {

        var start = document.getElementById('startvalue').value;
        var end = document.getElementById('endvalue').value;
        var request = {
            origin: start,
            destination: end,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
        directionsService.route(request, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {


    function btnDirections_onclick() {

    window.onload = InitializeMap;
<table id ="control">
<td class="style1">
    <input id="startvalue" type="text" style="width: 305px" /></td>
<td><input id="endvalue" type="text" style="width: 301px" /></td>
<td align ="right">
    <input id="btnDirections" type="button" value="GetDirections"
        onclick="return btnDirections_onclick()" /></td>
<td valign ="top">
<div id ="directionpanel"  style="height: 390px;overflow: auto" ></div>
<td valign ="top">
<div id ="Gmap" style="height: 390px; width: 489px"></div>

Then build the application and run it , no need to write any code in GooglemapsDirection.aspx.cs file.

Then you will get output like the following:

For Example if you enter Hyderabad in From textbox ,Mumbai in To textbox and click Find button then you will get the following output with Hyderabad to Mumbai direction and Distance:

Thank you….


  1. nice code thank you..........

  2. Can you tell me how to share these directions by email...?

  3. Thank you. this code is working.... But i want to load the map with my source and destination when pageload. If you know plz post it..

  4. how to get distance separate

  5. Oops! Something went wrong.
    This page didn't load Google Maps correctly. See the JavaScript console for technical details.
    code show this Error how to solve it con any body define????