gpt4 book ai didi

javascript - 将谷歌地图上的单位从公制更改为英制

转载 作者:行者123 更新时间:2023-11-29 20:01:16 24 4
gpt4 key购买 nike

我有以下代码:

<script>

var rendererOptions = {
draggable: false
};
var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);;
var directionsService = new google.maps.DirectionsService();
var map;

var England = new google.maps.LatLng(53.7415, 1.6860);

function initialize() {

var mapOptions = {
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: England
};
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
map.setTilt(45);
directionsDisplay.setMap(map)
directionsDisplay.setPanel(document.getElementById('directionsPanel'));

google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
computeTotalDistance(directionsDisplay.directions);
});

calcRoute();
}

function calcRoute() {

var request = {
origin: 'postcode',
destination: 'postcode',
waypoints:[{location: 'waypoint postcode'}, {location: 'waypoint postcode'}, {location: 'waypoint postcode'}],
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}

function computeTotalDistance(result) {
var total = 0;
var myroute = result.routes[0];
for (i = 0; i < myroute.legs.length; i++) {
total += myroute.legs[i].distance.value;
}
total = total / 1000.
document.getElementById('total').innerHTML = total + ' km';
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="float:left;width:70%; height:100%"></div>
<div id="directionsPanel" style="float:right;width:30%;height 100%">
<p>Total Distance: <span id="total"></span></p>
</div>

map ,使用 google api 的方向服务显示起点和终点,沿途有特定的路标。方向面板以公制单位显示所有距离。我该如何更改它,以便所有距离都以英制单位显示,例如英里、英尺?

最佳答案

From the docs

Unit Systems

By default, directions are calculated and displayed using the unit system of the origin's country or region. (Note: origins expressed using latitude/longitude coordinates rather than addresses always default to metric units.) For example, a route from "Chicago, IL" to "Toronto, ONT" will display results in miles, while the reverse route will display results in kilometers. You may override this unit system by setting one explicitly within the request using one of the following UnitSystem values:

  • UnitSystem.METRIC specifies usage of the metric system. Distances are shown using kilometers.
  • UnitSystem.IMPERIAL specifies usage of the Imperial (English) system. Distances are shown using miles.

Note: this unit system setting only affects the text displayed to the user. The directions result also contains distance values, not shown to the user, which are always expressed in meters.

关于javascript - 将谷歌地图上的单位从公制更改为英制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14404066/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com