从Google地图中的路线路线获取开始和结束地理编码

时间:2016-07-31 12:20:58

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

我根据自己的需要自定义 this google maps direction示例。
我想知道路线中 Origin 目的地点的 Lat / 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>

1 个答案:

答案 0 :(得分:0)

每条腿的起点和终点坐标在结果中的DirectionsLeg对象中可用。

DirectionsLeg对象的文档:

  

google.maps.DirectionsLeg对象规范

     

由DirectionsResult中的一组步骤组成的单条腿。对于所有请求,可能不会返回腿中的某些字段。请注意,虽然此结果是“类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>";
     }
  }

proof of concept fiddle

代码段

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>