如果表格与航路点一起使用,则距离"和"成本"算得很好。然而,如果有航路点,则计算仅在" start"之间进行。地址和第一个航点。它完全忽略了目的地和其他方式。
var inputs = document.getElementById('start');
var autocompletes = new google.maps.places.Autocomplete(inputs, options);
var inpute = document.getElementById('end');
var autocompletee = new google.maps.places.Autocomplete(inpute, options);
directionsDisplay = new google.maps.DirectionsRenderer();
var melbourne = new google.maps.LatLng(-31.953512, 115.857048);
var myOptions = {
zoom:12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: melbourne
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
directionsDisplay.setMap(map);
}
function calcRoute() {
var start = document.getElementById('start').value;
var end = document.getElementById('end').value;
if (waypoints.value) {
var waypts = [];
var optionsArray = document.getElementById("waypoints").value.split('\n');
for (var i = 0; i < optionsArray.length; i++) {
if(optionsArray.length>0) {
waypts.push({
location:optionsArray[i],
stopover:true});
}
}
}
var distanceInput = document.getElementById("distance");
var costInput = document.getElementById("cost");
if (waypoints.value) {
var request = {
origin:start,
destination:end,
waypoints: waypts,
optimizeWaypoints: true,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
} else {
var request = {
origin:start,
destination:end,
optimizeWaypoints: true,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
}
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
distanceInput.value = response.routes[0].legs[0].distance.value / 1000;
costInput.value = (2 * (response.routes[0].legs[0].distance.value / 1000));
}
});
}
</script>
</head>
<body onload="initialize()">
<div>
<p>
<label for="start">Pickup from: </label>
<input type="text" name="start" id="start" autocomplete="on" size="40"/>
<label for="end">Delivered to: </label>
<input type="text" name="end" id="end" autocomplete="on" size="40"/><BR>
Other Deliveries:<BR>
<textarea id="waypoints" name="waypoints" onclick="this.value='';" cols="10" rows="10">Address, Suburb, State
(Address is Optional)
New line for each address</textarea>
</p>
<p>
<div id="ins"></div>
</p>
<p>
<input type="submit" value="Calculate" onclick="if (waypoints.value == 'Address, Suburb, State\n(Address is Optional)\nNew line for each address') { waypoints.value=''}; calcRoute()" />
</p>
<p>
<label for="distance">Distance (km): </label>
<input type="text" name="distance" id="distance" readonly="true" />
</p>
<p>
<label for="cost">MAX cost: 1 standard item going direct</label>
<input type="text" name="cost" id="cost" readonly="true"/>
</p>
</div>
<div id="map_canvas" style="width:300px; height:200px;"></div>
我知道: distanceInput.value = response.routes [0] .legs [0] .distance.value / 1000; costInput.value =(2 *(response.routes [0] .legs [0] .distance.value / 1000)); 进行计算。但我所拥有的并不是一起计算它们。
谢谢
答案 0 :(得分:0)
如果添加航点,则response.routes [0] .legs中有多个元素。
所以距离应按此计算
{{1}}