Question

As I am new to Google Map api Javascript.I would like show distance Between Origin -- Destination.

The "First Input-Box "origin Could be Auto-Complete Box it Could Populate User suggested values through Google Map.

The "Second Input-box" Destination Could be User need to select list of place Already as Given.

I can't Figure out This.I need your help.

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
    <script>
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer();
  var chicago = new google.maps.LatLng(41.850033, -87.6500523);
  var mapOptions = {
    zoom:7,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: chicago
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  directionsDisplay.setMap(map);
  var inputFrom = /** @type {HTMLInputElement} */(document.getElementById('start'));
      var options = {
              componentRestrictions: {country: "UK"}
     };
  var autocompleteFrom = new google.maps.places.Autocomplete(inputFrom, options);

  var inputTo = /** @type {HTMLInputElement} */(document.getElementById('end'));
   var autocompleteFrom = new google.maps.places.Autocomplete(inputFrom, options);

  autocompleteFrom.bindTo('bounds', map);

   var infowindow = new google.maps.InfoWindow();
  var marker = new google.maps.Marker({
    map: map
  });

  google.maps.event.addListener(autocompleteFrom, 'place_changed', function() {
    infowindow.close();
    marker.setVisible(false);
    inputFrom.className = '';
    var place = autocompleteFrom.getPlace();
    if (!place.geometry) {
      // Inform the user that the place was not found and return.
      inputFrom.className = 'notfound';
      return;
    }

    // If the place has a geometry, then present it on a map.
    if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
    } else {
      map.setCenter(place.geometry.location);
      map.setZoom(17);  // Why 17? Because it looks good.
    }
    marker.setIcon(/** @type {google.maps.Icon} */({
      url: place.icon,
      size: new google.maps.Size(71, 71),
      origin: new google.maps.Point(0, 0),
      anchor: new google.maps.Point(17, 34),
      scaledSize: new google.maps.Size(35, 35)
    }));
    marker.setPosition(place.geometry.location);
    marker.setVisible(true);

    var address = '';
    if (place.address_components) {
      address = [
        (place.address_components[0] && place.address_components[0].short_name || ''),
        (place.address_components[1] && place.address_components[1].short_name || ''),
        (place.address_components[2] && place.address_components[2].short_name || '')
      ].join(' ');
    }

    infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
    infowindow.open(map, marker);




  });
  autocompleteTo.bindTo('bounds', map);

   var infowindow = new google.maps.InfoWindow();
  var marker = new google.maps.Marker({
    map: map
  });

  google.maps.event.addListener(autocompleteTo, 'place_changed', function() {
    infowindow.close();
    marker.setVisible(false);
    inputFrom.className = '';
    var place = autocompleteTo.getPlace();
    if (!place.geometry) {
      // Inform the user that the place was not found and return.
      inputTo.className = 'notfound';
      return;
    }

    // If the place has a geometry, then present it on a map.
    if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
    } else {
      map.setCenter(place.geometry.location);
      map.setZoom(17);  // Why 17? Because it looks good.
    }
    marker.setIcon(/** @type {google.maps.Icon} */({
      url: place.icon,
      size: new google.maps.Size(71, 71),
      origin: new google.maps.Point(0, 0),
      anchor: new google.maps.Point(17, 34),
      scaledSize: new google.maps.Size(35, 35)
    }));
    marker.setPosition(place.geometry.location);
    marker.setVisible(true);

    var address = '';
    if (place.address_components) {
      address = [
        (place.address_components[0] && place.address_components[0].short_name || ''),
        (place.address_components[1] && place.address_components[1].short_name || ''),
        (place.address_components[2] && place.address_components[2].short_name || '')
      ].join(' ');
    }

    infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
    infowindow.open(map, marker);
    calcRoute();

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



google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="panel">
    <b>Start: </b>
    <input id="start"  type="text" placeholder="Heathrow Airport" size="30">
    <b>End: </b>

    <select id="end" onchange="calcRoute();">
      <option value="chicago, il">Chicago</option>
      <option value="st louis, mo">St Louis</option>
      <option value="joplin, mo">Joplin, MO</option>
      <option value="oklahoma city, ok">Oklahoma City</option>
      <option value="amarillo, tx">Amarillo</option>
      <option value="gallup, nm">Gallup, NM</option>
      <option value="flagstaff, az">Flagstaff, AZ</option>
      <option value="winona, az">Winona</option>
      <option value="kingman, az">Kingman</option>
      <option value="barstow, ca">Barstow</option>
      <option value="san bernardino, ca">San Bernardino</option>
      <option value="los angeles, ca">Los Angeles</option> 

No correct solution

OTHER TIPS

This information is stored in the distance-property of the legs of a route.

A successfull DirectionsResponse for a request without provideRouteAlternatives and waypoints returns a single route with 1 leg, so you may access the distance inside calcRoute via:

response.routes[0].legs[0].distance.text
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top