我想比较使用高速公路和避开高速公路时的距离,但是我不知道该怎么做。我的代码正在计算两点之间的距离,并根据距离计算价格。如果选中了高速公路复选框,则应该计算不同的价格,但是当没有高速公路时,价格应保持不变。
这是代码
var directionsDisplay = new google.maps.DirectionsRenderer();
var directionsService = new google.maps.DirectionsService();
var map;
function initialize() {
var myOptions = {
center: new google.maps.LatLng(43.521573, 16.441015),
zoom: 5,
mapTypeID: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'), myOptions);
directionsDisplay.setMap(map);
google.maps.event.trigger(map, 'resize');
}
function calcRoute() {
var start = document.getElementById('start').value;
var end = document.getElementById('end').value;
var vehicle = document.getElementById('vehicle').value;
//var highway = document.getElementById('highway').checked;
//var round = document.getElementById('return').checked;
var distanceInput = document.getElementById('distance');
var timeInput = document.getElementById('time');
var priceInput = document.getElementById('price');
var request, price;
if (document.getElementById('highway').checked) {
request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING,
avoidHighways: false
};
} else {
request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING,
avoidHighways: true
};
}
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
if (document.getElementById('highway').checked)
dist_highway = response.routes[0].legs[0].distance.value / 1000;
else
dist = response.routes[0].legs[0].distance.value / 1000;
distanceInput.value = response.routes[0].legs[0].distance.value / 1000;
var hours = parseInt( response.routes[0].legs[0].duration.value / 3600) % 24;
var minutes = parseInt( response.routes[0].legs[0].duration.value / 60) % 60;
timeInput.value = hours + "h " + minutes + "m";
if (vehicle == 'van') {
if ((response.routes[0].legs[0].distance.value / 1000) < '30' ) {
price = (response.routes[0].legs[0].distance.value / 1000) * 1.19;
} else {
price = (response.routes[0].legs[0].distance.value / 1000) * 0.93;
}
} else if (vehicle == 'personal') {
if ((response.routes[0].legs[0].distance.value / 1000) < '30' ) {
price = (response.routes[0].legs[0].distance.value / 1000) * 1.32;
} else {
price = (response.routes[0].legs[0].distance.value / 1000) * 1.06;
}
}
if (document.getElementById('highway').checked && vehicle == 'personal' ) {
price *= 1.3;
} else if (document.getElementById('highway').checked && vehicle == 'van' ) {
price *= 1.5;
};
if (document.getElementById('return').checked) {
price *= 1.7;
};
priceInput.value = price.toFixed(2) + "€";
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
经过AlexB的解释,这是我的职责。问题解决了。
function calcDist(start, end) {
var requestHighway, request, distHighway, dist;
var dirService = new google.maps.DirectionsService();
requestHighway = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING,
avoidHighways: false
};
request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING,
avoidHighways: true
};
dirService.route(requestHighway, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
distHighway = response.routes[0].legs[0].distance.value / 1000;
distHighway = distHighway.toFixed(2);
}
});
dirService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
dist = response.routes[0].legs[0].distance.value / 1000;
dist = distHighway.toFixed(2);
}
});
if (distHighway - dist == 0) {
return true;
} else {
return false;
}
}
最佳答案
经过AlexB的解释,这是我的职责。问题解决了。
function calcDist(start, end) {
var requestHighway, request, distHighway, dist;
var dirService = new google.maps.DirectionsService();
requestHighway = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING,
avoidHighways: false
};
request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING,
avoidHighways: true
};
dirService.route(requestHighway, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
distHighway = response.routes[0].legs[0].distance.value / 1000;
distHighway = distHighway.toFixed(2);
}
});
dirService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
dist = response.routes[0].legs[0].distance.value / 1000;
dist = distHighway.toFixed(2);
}
});
if (distHighway - dist == 0) {
return true;
} else {
return false;
}
}
关于javascript - 比较高速公路并避开高速公路距离,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23995719/