如何在我的javascript

时间:2015-07-28 15:00:39

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

如何在我的javascript中获得KM中的总距离。它已经显示在Panel的顶部,但我也需要在我的javascript中使用此值。我的javascript如下:

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

   }
 }); 

功能正常,但我还想提醒总距离。这可以在面板的顶部看到。该面板将来是不可见的,因此getelementbyID功能是不够的。

有人可以帮我这个吗?

谢谢!

塞尔吉奥

4 个答案:

答案 0 :(得分:1)

根据documentation

  

unitSystem

     

输入:UnitSystem

     

显示距离时使用的首选单位制。默认为原产国使用的单位制。

所以如果你想强迫它去METRIC:

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

这将强制response.routes[0].legs[0].distance.text的值使用公制系统。

正如@geocodezip回答中所解释的那样,你也可以依赖总是以米为单位返回的response.routes[0].legs[0].distance.value,即使原籍国使用英制系统。

答案 1 :(得分:1)

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>

答案 2 :(得分:1)

您可以通过以下方式轻松获得距离:

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

例如,结果将是“20,5 km”之类的文本。

答案 3 :(得分:0)

在您的请求中,设置units=metric

然后在警报中使用:distance = response.routes[0].legs[0].distance.text;