Google Maps V3 API: Calculate distance between two locations on TextBox blur

Last Reply one year ago By dharmendr

Posted one year ago


i want to use this snippet on textbox blur not on button click event please advice

Google Maps V3 API: Calculate distance between two addresses / points / locations

You are viewing reply posted by: dharmendr one year ago.
Posted one year ago

Hi nauna,

Refer below code.


<script type="text/javascript" src=""></script>
<script type="text/javascript">
    var source, destination;
    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();
    google.maps.event.addDomListener(window, 'load', function () {
        new google.maps.places.SearchBox(document.getElementById('txtSource'));
        new google.maps.places.SearchBox(document.getElementById('txtDestination'));
        directionsDisplay = new google.maps.DirectionsRenderer({ 'draggable': true });

    function GetRoute() {
        var mumbai = new google.maps.LatLng(18.9750, 72.8258);
        var mapOptions = {
            zoom: 7,
            center: mumbai
        map = new google.maps.Map(document.getElementById('dvMap'), mapOptions);

        //*********DIRECTIONS AND ROUTE**********************//
        source = document.getElementById("txtSource").value;
        destination = document.getElementById("txtDestination").value;

        var request = {
            origin: source,
            destination: destination,
            travelMode: google.maps.TravelMode.DRIVING
        directionsService.route(request, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {

        //*********DISTANCE AND DURATION**********************//
        var service = new google.maps.DistanceMatrixService();
            origins: [source],
            destinations: [destination],
            travelMode: google.maps.TravelMode.DRIVING,
            unitSystem: google.maps.UnitSystem.METRIC,
            avoidHighways: false,
            avoidTolls: false
        }, function (response, status) {
            if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {
                var distance = response.rows[0].elements[0].distance.text;
                var duration = response.rows[0].elements[0].duration.text;
                var dvDistance = document.getElementById("dvDistance");
                dvDistance.innerHTML = "";
                dvDistance.innerHTML += "Distance: " + distance + "<br />";
                dvDistance.innerHTML += "Duration:" + duration;

            } else {
                alert("Unable to find the distance via road.");
<table border="0" cellpadding="0" cellspacing="3">
        <td colspan="2">
            <input type="text" id="txtSource" value="" style="width: 200px" onblur="GetRoute()" />
            <br />
            <input type="text" id="txtDestination" value="" style="width: 200px" onblur="GetRoute()" />
            <hr />
        <td colspan="2">
            <div id="dvDistance">
            <div id="dvMap" style="width: 500px; height: 500px">
            <div id="dvPanel" style="width: 500px; height: 500px">