从谷歌地图中的路线中获取起点和终点地理代码

Get start and end geocode from the directions route in google maps

本文关键字:终点 代码 起点 获取 谷歌地图      更新时间:2023-09-26

我正在根据需要定制这个google maps direction示例
我想知道路线上起点终点纬度/Lng。如何获取这些坐标?

我一直在调试响应,但找不到这些坐标。

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 4,
    center: {lat: -24.345, lng: 134.46}  // Australia.
  });
  var directionsService = new google.maps.DirectionsService;
  var directionsDisplay = new google.maps.DirectionsRenderer({
    draggable: true,
    map: map,
    panel: document.getElementById('right-panel')
  });
  directionsDisplay.addListener('directions_changed', function() {
    computeTotalDistance(directionsDisplay.getDirections());
  });
  displayRoute('Perth, WA', 'Sydney, NSW', directionsService,
      directionsDisplay);
}
function displayRoute(origin, destination, service, display) {
  service.route({
    origin: origin,
    destination: destination,
    waypoints: [{location: 'Adelaide, SA'}, {location: 'Broken Hill, NSW'}],
    travelMode: 'DRIVING',
    avoidTolls: true
  }, function(response, status) {
    if (status === 'OK') {
      display.setDirections(response);
    } else {
      alert('Could not display directions due to: ' + status);
    }
  });
}
function computeTotalDistance(result) {
  var total = 0;
  var myroute = result.routes[0];
  for (var i = 0; i < myroute.legs.length; i++) {
    total += myroute.legs[i].distance.value;
  }
  total = total / 1000;
  document.getElementById('total').innerHTML = total + ' km';
}
#right-panel {
  font-family: 'Roboto','sans-serif';
  line-height: 30px;
  padding-left: 10px;
}
#right-panel select, #right-panel input {
  font-size: 15px;
}
#right-panel select {
  width: 100%;
}
#right-panel i {
  font-size: 12px;
}
html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}
#map {
  height: 100%;
  float: left;
  width: 63%;
  height: 100%;
}
#right-panel {
  float: right;
  width: 34%;
  height: 100%;
}
.panel {
  height: 100%;
  overflow: auto;
}
<div id="map"></div>
<div id="right-panel">
  <p>Total Distance: <span id="total"></span></p>
</div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer
src="https://maps.googleapis.com/maps/api/js?callback=initMap">
</script>

结果中的DirectionsLeg对象中提供了每条腿的开始和结束坐标。

DirectionsLeg对象的文档:

google.maps.DirectionsLeg对象规范

由DirectionsResult中的一组步骤组成的单个分支。分支中的某些字段可能不会针对所有请求返回。注意,尽管该结果是"0";类似JSON;它不是严格意义上的JSON,因为它直接和间接地包括LatLng对象。

end_location|类型:LatLng

DirectionsService通过在起点和终点使用最近的交通选项(通常是道路)来计算地点之间的方向。end_location表示实际的地理编码目的地,例如,如果道路不在该路段的目的地附近,则其可能不同于上一步的end_location。

start_location|类型:LatLng

DirectionsService通过在起点和终点使用最近的交通选项(通常是道路)来计算地点之间的方向。start_location表示实际的地理编码原点,例如,如果道路不在该路段的原点附近,则其可能不同于第一步的start_location。

解析失控响应的示例代码:

  for (var i=0;i<response.routes.length;i++){
     for (var j=0;j<response.routes[i].legs.length; j++) {
        document.getElementById('info').innerHTML += "["+j+"] start coord="+response.routes[i].legs[j].start_location.toUrlValue(6)+"<br>";
        document.getElementById('info').innerHTML += "["+j+"] end coord="+response.routes[i].legs[j].end_location.toUrlValue(6)+"<br>";
     }
  }

概念验证小提琴

代码片段:

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 4,
    center: {
      lat: -24.345,
      lng: 134.46
    } // Australia.
  });
  var directionsService = new google.maps.DirectionsService;
  var directionsDisplay = new google.maps.DirectionsRenderer({
    draggable: true,
    map: map,
    panel: document.getElementById('right-panel')
  });
  directionsDisplay.addListener('directions_changed', function() {
    computeTotalDistance(directionsDisplay.getDirections());
  });
  displayRoute('Perth, WA', 'Sydney, NSW', directionsService,
    directionsDisplay);
}
function displayRoute(origin, destination, service, display) {
  service.route({
    origin: origin,
    destination: destination,
    waypoints: [{
      location: 'Adelaide, SA'
    }, {
      location: 'Broken Hill, NSW'
    }],
    travelMode: 'DRIVING',
    avoidTolls: true
  }, function(response, status) {
    if (status === 'OK') {
      display.setDirections(response);
      for (var i = 0; i < response.routes.length; i++) {
        for (var j = 0; j < response.routes[i].legs.length; j++) {
          document.getElementById('info').innerHTML += "[" + j + "] start coord=" + response.routes[i].legs[j].start_location.toUrlValue(6) + "<br>";
          document.getElementById('info').innerHTML += "[" + j + "] end&nbsp; coord=" + response.routes[i].legs[j].end_location.toUrlValue(6) + "<br>";
        }
      }
    } else {
      alert('Could not display directions due to: ' + status);
    }
  });
}
function computeTotalDistance(result) {
  var total = 0;
  var myroute = result.routes[0];
  for (var i = 0; i < myroute.legs.length; i++) {
    total += myroute.legs[i].distance.value;
  }
  total = total / 1000;
  document.getElementById('total').innerHTML = total + ' km';
}
#right-panel {
  font-family: 'Roboto', 'sans-serif';
  line-height: 30px;
  padding-left: 10px;
}
#right-panel select,
#right-panel input {
  font-size: 15px;
}
#right-panel select {
  width: 100%;
}
#right-panel i {
  font-size: 12px;
}
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
#map {
  height: 100%;
  float: left;
  width: 63%;
  height: 100%;
}
#right-panel {
  float: right;
  width: 34%;
  height: 100%;
}
.panel {
  height: 100%;
  overflow: auto;
}
<div id="info"></div>
<div id="map"></div>
<div id="right-panel">
  <p>Total Distance: <span id="total"></span>
  </p>
</div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap">
</script>