【问题标题】:Google map not working properly谷歌地图无法正常工作
【发布时间】:2016-09-28 06:39:49
【问题描述】:

我想使用谷歌地图从 2 个点构建距离计算器。但输出不起作用。我没有得到这 2 个点的距离和持续时间 这是我的 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('autocomplete'));
  new google.maps.places.SearchBox(document.getElementById('autocomplete1'));
  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);
  directionsDisplay.setMap(map);
  directionsDisplay.setPanel(document.getElementById('dvPanel'));

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

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

  //*********DISTANCE AND DURATION**********************//
  var service = new google.maps.DistanceMatrixService();
  service.getDistanceMatrix({
    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.");
    }
  });
} 

这是我的html代码

<div id="fifth">
  <div class="row">
    <p><b>Step 5:Locations Details</b></p>
  </div>
  <div class="row">
    <div class="col-md-3">
      <input type="text" class="form-control" id="autocomplete"  placeholder="Pick-up Location">
    </div>  
    <div class="col-md-3">
      <input type="text" class="form-control" id="autocomplete1"  placeholder="Drop-off Location">
    </div> 
    <div class="col-md-3">
      <input type="button" class="form-control" value="Get Distance" onclick="GetRoute()">
    </div>  
  </div>
  <div class="row">
    <table>
      <tr>
        <td colspan="2">
          <div id="dvDistance"></div>
        </td>
      </tr>
      <tr>
        <td>
          <div id="dvMap" style="width: 500px; height: 500px"></div>
        </td>
        <td>
          <div id="dvPanel" style="width: 500px; height: 500px"></div>
        </td>
      </tr>
    </table>
  </div>
</div>

我是编程新手。有人可以帮助我吗?对不起我的英语不好

【问题讨论】:

标签: javascript html google-maps google-maps-api-3 maps


【解决方案1】:

您是否指定了“libraries=places”?

src="https://maps.googleapis.com/maps/api/js?key=123456&callback=initMap&libraries=places"

【讨论】:

    猜你喜欢
    • 2018-01-02
    • 2011-03-25
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多