所以基本上我有这个脚本(信用到1stwebdesigner.com和geocodezip),它计算4个位置之间的距离。 位置1,位置2,位置3,位置4。 我现在需要计算location2和位置3之间的距离。 并获得该距离并显示它并根据此公式显示价格
我该怎么做?
到目前为止,我有这个(致Rajaprabhu的积分) (Price Calculation based on the distance covered) 问题1是,我的情况公式是错误的,问题2是公式与前一次计算没有距离。
var request = {
origin:location2,
destination:location3,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status)
{
if (status == google.maps.DirectionsStatus.OK)
{
var distance = response.routes[0].legs[0].distance.text;
var duration = response.routes[0].legs[0].duration.text;
var dvDistance = document.getElementById("dvDistance");
dvDistance.innerHTML = "";
dvDistance.innerHTML += "The one way Distance is: " + distance + "<br />";
dvDistance.innerHTML += "The one way Duration is: " + duration;
//calculate the one way price using the klms
var kms = distance;
var price_1 = (kms > 0) ? 3 : 0; kms = (kms > 0)? kms - 1 : 0;
var price_2 = (kms - 14) > 0 ? (14 * 1.60) : (kms * 1.60); kms = (kms-14)>0 ? kms - 14 : 0;
var price_3 = (kms - 15) > 0 ? (15 * 1.40) : (kms * 1.40); kms = (kms-15)>0 ? kms - 15 : 0;
var price_4 = (kms > 0) ? (kms * 1.20) : 0;
document.getElementById("displayprice").innerHTML = "the one way price is: " + (price_1 + price_2 + price_3 + price_4);
}
});
现在坚持公式。 1.超过35公里的任何结果都是错误的。 2. 10公里及以下应该默认为99美元。
//calculate the one way price using the klms
var kms = distance;
console.log(kms);
var price_1 = (kms > 0) ? 99 : 0; kms = (kms > 0)? kms - 10 : 0;
var price_2 = (kms - 10) > 0 ? (10 * 5.00) : (kms * 5.00); kms = (kms-10)>0 ? kms - 10 : 0;
var price_3 = (kms - 20) > 0 ? (15 * 3.75) : (kms * 3.75); kms = (kms-20)>0 ? kms - 20 : 0;
var price_4 = (kms > 0) ? (kms * 3.50) : 0;
答案 0 :(得分:1)
google.maps.Distance对象规范
将距离表示为数值和显示字符串。
属性
文字 |输入:string |使用请求中指定的UnitSystem的距离值的字符串表示。
值 |类型:数字|以米为单位的距离。
您的距离是一个字符串:var distance = response.routes[0].legs[0].distance.text;
您应该使用数值(以米为单位):
var distance = response.routes[0].legs[0].distance.value/1000;
代码段
var geocoder;
var map;
// New York, NY, USA (40.7127837, -74.00594130000002)
// Newark, NJ, USA (40.735657, -74.1723667)
// Philadelphia, PA, USA (39.9525839, -75.16522150000003)
// Baltimore, MD, USA (39.2903848, -76.61218930000001)
var location1 = new google.maps.LatLng(40.7127837, -74.005941);
var location2 = new google.maps.LatLng(40.735657, -74.1723667);
var location3 = new google.maps.LatLng(39.9525839, -75.1652215);
var location4 = new google.maps.LatLng(39.2903848, -76.6121893);
function initialize() {
// create a new map object
// set the div id where it will be shown
// set the map options
var mapOptions = {
center: {
lat: 42,
lng: -72
},
zoom: 4
}
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
// show route between the points
directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer({
suppressMarkers: true,
suppressInfoWindows: true
});
directionsDisplay.setMap(map);
var request = {
origin: location1,
waypoints: [{
location: location2,
}, {
location: location3,
}],
destination: location4,
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.
for (var i = 0; i < route.legs.length; i++) {
var routeSegment = i + 1;
summaryPanel.innerHTML += "<b>Route Segment: " + routeSegment + "</b><br />";
summaryPanel.innerHTML += route.legs[i].start_address + " to ";
summaryPanel.innerHTML += route.legs[i].end_address + "<br />";
summaryPanel.innerHTML += route.legs[i].distance.text + "<br />";
summaryPanel.innerHTML += route.legs[i].duration.text + "<br />";
}
computeTotalDistance(response);
var request = {
origin: location2,
destination: location3,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
var distance = response.routes[0].legs[0].distance.value / 1000;
var duration = response.routes[0].legs[0].duration.text;
var dvDistance = document.getElementById("dvDistance");
dvDistance.innerHTML = "";
dvDistance.innerHTML += "The one way Distance (segment 2) is: " + distance + "<br />";
dvDistance.innerHTML += "The one way Duration (segment 2) is: " + duration;
//calculate the one way price using the klms
var kms = distance;
console.log(kms);
var price_1 = (kms > 0) ? 3 : 0;
kms = (kms > 0) ? kms - 1 : 0;
var price_2 = (kms - 14) > 0 ? (14 * 1.60) : (kms * 1.60);
kms = (kms - 14) > 0 ? kms - 14 : 0;
var price_3 = (kms - 15) > 0 ? (15 * 1.40) : (kms * 1.40);
kms = (kms - 15) > 0 ? kms - 15 : 0;
var price_4 = (kms > 0) ? (kms * 1.20) : 0;
document.getElementById("displayprice").innerHTML = "the one way price (segment 2) is: $" + (price_1 + price_2 + price_3 + price_4).toFixed(2);
}
});
} else {
alert("directions response " + status);
}
});
}
google.maps.event.addDomListener(window, "load", initialize);
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(2) + " minutes<br>total price is: $" + ((totalTime / 60).toFixed(2) * 2.1) + " dollars<br>saturday price is: $" + ((totalTime / 60).toFixed(2) * 2.35) + " dollars<br>sunday price is: $" + ((totalTime / 60).toFixed(2) * 2.6) + " dollars";
}
&#13;
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>
<div id="dvDistance"></div>
<div id="displayprice"></div>
<div id="total"></div>
<div id="directions_panel"></div>
&#13;