How can i get the total distance in KM in my javascript

独自空忆成欢 提交于 2019-12-25 14:28:47

问题


How can i get the total distance in KM in my javascript. It is shown already on top of the Panel, but i need this value also in my javascript. My javascript is as followed:

 var directionsService = new google.maps.DirectionsService();
 var directionsDisplay = new google.maps.DirectionsRenderer();

 var map = new google.maps.Map(document.getElementById('map'), {
   zoom:7,
   mapTypeId: google.maps.MapTypeId.ROADMAP
 });

 directionsDisplay.setMap(map);
 directionsDisplay.setPanel(document.getElementById('panel'));


 var een = '52.3641205';
 var twee = '4.905697000000032';
 var drie = '52.6010666';
 var vier =  '4.73768229999996';

 var p1 = new google.maps.LatLng(een, twee);
 var p2 = new google.maps.LatLng(drie, vier);

 var request = {
   origin: p1, 
   destination: p2,
   travelMode: google.maps.DirectionsTravelMode.DRIVING
 };

 directionsService.route(request, function(response, status) {
   if (status == google.maps.DirectionsStatus.OK) {

     alert('Total travel distance is:' + response);

     directionsDisplay.setDirections(response);

   }
 }); 

The functionality is working but i would also like to alert the total distance. This is seen at the top of the panel. The panel will be invisible in the future so a getelementbyID function is not enough.

Can someone help me with this?

Thank You!

Sergio


回答1:


In your request, set units=metric.

Then use: distance = response.routes[0].legs[0].distance.text; in your alert.




回答2:


The distance is available in each of the route legs

google.maps.Distance object specification
A representation of distance as a numeric value and a display string.

Properties  Type    Description
text    string  A string representation of the distance value, using the UnitSystem specified in the request.
value   number  The distance in meters.

proof of concept fiddle

code snippet:

var geocoder;
var map;

function initialize() {
  var directionsService = new google.maps.DirectionsService();
  var directionsDisplay = new google.maps.DirectionsRenderer();

  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 7,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });

  directionsDisplay.setMap(map);
  directionsDisplay.setPanel(document.getElementById('panel'));


  var een = '52.3641205';
  var twee = '4.905697000000032';
  var drie = '52.6010666';
  var vier = '4.73768229999996';

  var p1 = new google.maps.LatLng(een, twee);
  var p2 = new google.maps.LatLng(drie, vier);

  var request = {
    origin: p1,
    destination: p2,
    travelMode: google.maps.DirectionsTravelMode.DRIVING
  };

  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {

      document.getElementById('distance').innerHTML = 'Total travel distance is: ' + (response.routes[0].legs[0].distance.value / 1000).toFixed(2) + " km";
      alert('Total travel distance is: ' + (response.routes[0].legs[0].distance.value / 1000).toFixed(2) + ' km');
      directionsDisplay.setDirections(response);

    }
  });
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="distance"></div>
<div id="map" style="border: 2px solid #3872ac;"></div>
<div id="panel"></div>



回答3:


As per the documentation:

unitSystem

Type: UnitSystem

Preferred unit system to use when displaying distance. Defaults to the unit system used in the country of origin.

So if you want to force it to METRIC:

var request = {
    origin: p1,
    destination: p2,
    travelMode: google.maps.DirectionsTravelMode.DRIVING,
    unitSystem: google.maps.UnitSystem.METRIC
};

This will force the value of response.routes[0].legs[0].distance.text to use the metric system.

And as explained in @geocodezip answer, you can also rely on the response.routes[0].legs[0].distance.value which is always returned in meters, even if the country of origin uses the imperial system.




回答4:


You can easily get the distance by getting:

directionsDisplay.directions.routes[0].legs[0].distance.text

The result, for example, would be text like "20,5 km".



来源:https://stackoverflow.com/questions/31680105/how-can-i-get-the-total-distance-in-km-in-my-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!