没有显示距离和距离到(折线)

时间:2013-11-08 12:42:39

标签: google-maps-api-3 service distance polyline autocompletebox

因为我是谷歌地图api Javascript的新手。我想显示原点 - 目的地之间的距离。

“第一个输入框”原点可以是自动完成框,可以通过谷歌地图填充用户建议的值。

“第二个输入框”目的地可能是用户需要选择已经给定的地点列表。

我无法想出这个。我需要你的帮助。

<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> 

1 个答案:

答案 0 :(得分:1)

此信息存储在路线支路的距离属性中。

对于没有provideRouteAlternativeswaypoints的请求,成功的DirectionsResponse会返回单routeleg,因此您可以通过calcRoute访问response.routes[0].legs[0].distance.text 内的距离:

{{1}}