帮我解决这个案例 - 演示:http://jsfiddle.net/aLosek26/(在html中解释)
如何根据最后一个地址的预设到达时间计算每个站点的接送时间?
这是代码
<style>
html {
height: 100%
}
body {
height: 100%;
margin: 0px;
padding: 0px
}
.ziel {
padding: 10px;
background-color: #0086A9;
color: #fff;
font-size: 15px;
margin-bottom: 10px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maps.google.com/maps/api/js"></script>
<div id="map_canvas" style="float:left;width:70%;height:100%;"></div>
<div id="control_panel" style="float:right;width:30%;text-align:left;padding-top:20px">
<div id="directions_panel" style="margin:20px;background-color:#FFEE77;font-family:Gotham, 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:12px;padding:20px;"></div>
<div style="padding-left:10px;">I want "pick up time" to look like this:
<br> A to B -> pick up time: 13:12
<br> B to C -> pick up time: 13:16
<br> C to D -> pick up time: 13:48
<br> D to E -> pick up time: 14:20
<br> E to F -> pick up time: 14:32
<br> F to G -> pick up time: 14:45
<br>
<br> </div>
<div><strong>Arrival to the last address is 15:00 </strong> </div>
<br>
<br>
<br>
<div id="total"></div>
</div>
<script>
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var chicago = new google.maps.LatLng(41.850033, -87.6500523);
var myOptions = {
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: chicago
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
directionsDisplay.setMap(map);
calcRoute();
}
google.maps.event.addDomListener(window, 'load', initialize);
function calcRoute() {
var checkboxArray = ["Klosterneuburgerstrasse 3,1200 Wien", "Simon-Denk Gasse 2,1090 wien", "Favoritenstrasse 3, 1100 Wien", "Hütteldorferstrasse 4, 1140 Wien", "Summeringerhauptstrasse 3, 1110 Wien", "Kobesgase, 1220 Wien", "Ziegelhofstrasse 3, 1220 Wien"];
var start = checkboxArray[0];
var end = checkboxArray[checkboxArray.length - 1];
var waypts = [];
for(var i = 1; i < checkboxArray.length - 1; i++) {
var adresanesto = checkboxArray[i];
if(adresanesto !== "") {
waypts.push({
location: adresanesto,
stopover: true
});
}
}
var request = {
// from: Blackpool to: Preston to: Blackburn
origin: start,
destination: end,
waypoints: waypts,
optimizeWaypoints: true,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if(status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
var route = response.routes[0];
var summaryPanel = document.getElementById("directions_panel");
summaryPanel.innerHTML = "";
// For each route, display summary information.
var ankunft = "06:00";
for(var i = 0; i < route.legs.length; i++) {
var hms = ankunft; // your input string
var zeit = route.legs[i].duration.value;
var zeitx = (zeit / 60).toFixed(0);
var a = hms.split(':');
var sekunde = (+a[0]) * 60 * 60 + (+a[1]) * 60;
var min5x = zeitx * 60;
var sec_num = (+sekunde - +min5x);
var hours = Math.floor(sec_num / 3600);
var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
var seconds = Math.floor(sec_num - (hours * 3600) - (minutes * 60));
if(hours < 10) {
hours = "0" + hours;
}
if(minutes < 10) {
minutes = "0" + minutes;
}
if(seconds < 10) {
seconds = "0" + seconds;
}
var vreme = hours + ':' + minutes;
var routeSegment = i + 1;
var ruta = "ruta" + routeSegment;
var timespan = "timespan" + routeSegment;
var vremespan = "vremespan" + routeSegment;
if(routeSegment == 1) {
var ziel = 'A to B';
}
if(routeSegment == 2) {
var ziel = 'B to C';
}
if(routeSegment == 3) {
var ziel = 'C to D';
}
if(routeSegment == 4) {
var ziel = 'D to E';
}
if(routeSegment == 5) {
var ziel = 'E to F';
}
if(routeSegment == 6) {
var ziel = 'F to G';
}
if(routeSegment == 7) {
var ziel = 'G to H';
}
if(routeSegment == 8) {
var ziel = 'H to I';
}
summaryPanel.innerHTML += "<div id='" + ruta + "'><b><span class='ziel'> " + ziel + "</span></b>
";
// summaryPanel.innerHTML += route.legs[i].start_address + " to ";
// summaryPanel.innerHTML += route.legs[i].end_address + "
";
summaryPanel.innerHTML += "<b>Distance: </b>" + route.legs[i].distance.text + "
";
summaryPanel.innerHTML += "<b>Duration: </b><span id='" + timespan + "'>" + zeitx + "</span> Min.
";
summaryPanel.innerHTML += "<b>Pick up time: </b><span id='" + vremespan + "'><span style='color:#ff0000'>" + vreme + "</span> <---- ???
</div>";
}
computeTotalDistance(response);
} else {
alert("directions response " + status);
}
});
}
function computeTotalDistance(result) {
var totalDist = 0;
var totalTime = 0;
var myroute = result.routes[0];
for(i = 0; i < myroute.legs.length; i++) {
totalDist += myroute.legs[i].distance.value;
totalTime += myroute.legs[i].duration.value;
}
totalDist = totalDist / 1000.
document.getElementById("total").innerHTML = "total distance is: " + totalDist + " km<br>total time is: " + (totalTime / 60).toFixed(0) + " minutes";
}
</script>
编辑解释
我们有一个起始地址和结束地址以及四个航点,并且具有可变的“到达” - 到达时间
抵达15:00
A B C D E F G =不同的地址 - 街道号邮政编码城市......
A to B Distance: 1,2 km Duration: 4 Min.
B to C Distance: 13,8 km Duration: 32 Min.
C to D Distance: 20,3 km Duration: 32 Min.
D to E Distance: 6,9 km Duration: 12 Min.
E to F Distance: 9,1 km Duration: 13 Min.
F to G Distance: 6,9 km Duration: 15 Min.
A是起始地址B C D E,F是航路点G是结束地址
例:
结束地址的到达时间设置为15:00,航路点F和结束地址G之间的持续时间为15分钟,然后是从航路点F到14点45分的接收时间 . F到G的接送时间为14:45
如何计算所有航路点?
A到B接送时间?
B到C拿起时间?
...
我希望看起来像这样
A到B - 距离:1,2 km持续时间:4分钟接送时间:13:12
B to C - 距离:13,8 km时长:32分钟 . 接机时间:13:16
C至D - 距离:20,3 km持续时间:32分钟接机时间:13:48
D到E - 距离:6,9 km持续时间:12分钟接送时间:14:20
E到F - 距离:9,1 km持续时间:13分钟接送时间:14:32
F到G - 距离:6.9 km持续时间:15分钟接送时间:14:45
我需要这辆出租车
我希望你明白我想要的......
对不起,我的英语不好...
2 回答
我想我和Dr.Molle几乎一样(我们开始同时编写这段代码) . 我也从完成到开始迭代 .
但无论如何我想发布我的代码 .
我会在几秒钟内完成计算 . 只在最后一刻转换;并且不要使用自定义计算,例如(小时* 3600)...时间很棘手;你没有想到很多例外 .
example fiddle
当你不知道下一个腿的持续时间时,你将如何计算航点的起飞时间?
你必须从最后一条腿到第一条腿进行迭代 .
而不是使用复杂的时间计算,你应该使用
Date
-object,'s what it' s适合 .