我有这个代码:
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
var directionDisplay;
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var copenhagen = new google.maps.LatLng(55.6771, 12.5704);
var myOptions = {
zoom:12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: copenhagen
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
directionsDisplay.setMap(map);
}
var directionsService = new google.maps.DirectionsService();
function calcRoute() {
var start = document.getElementById("start").value;
var end = document.getElementById("end").value;
var distanceInput = document.getElementById("distance");
var request = {
origin:start,
destination:end,
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;
}
});
}
</script>
<title>Distance Calculator</title>
<style type="text/css">
body {
font-family:Helvetica, Arial;
}
#map_canvas {
height: 50%;
}
</style>
</head>
<body>
<body onload="initialize()">
<p>Enter your current location and desired destination to get the distance</p>
<div>
<p>
<label for="start">Start: </label>
<input type="text" name="start" id="start" />
<br>
<label for="end">End: </label>
<input type="text" name="end" id="end" />
<input type="submit" value="Calculate Route" onclick="calcRoute()" />
</p>
<p>
<script>
var cost=distance*2-(distance*2*0.20)
document.write("Cost: ");
document.write(cost + "<br>");
</script>
</p>
</div>
<div id="map_canvas"></div>
</body>
</html>
并且问题中的代码是:
<script>
var cost=distance*2-(distance*2*0.20)
document.write("Cost: ");
document.write(cost + "<br>");
</script>
此代码会询问用户输入起始地址和目的地地址,并使用Google Maps API计算两点之间的距离。然后我添加了功能来计算出租车使用他们的区域固定费率公式(公里距离)* 2-20%来花费多少费用。
我的问题在于,“距离”变量是在API代码中定义的,所以当我在计算成本时引用它时,它似乎不会继续...就像它没有甚至存在。我知道它与在尝试使用它的人不同的下标中定义它有关。任何和所有建议表示赞赏!
答案 0 :(得分:2)
我认为这就是你要找的东西。希望它有所帮助。
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
var directionDisplay;
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var copenhagen = new google.maps.LatLng(55.6771, 12.5704);
var myOptions = {
zoom:12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: copenhagen
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
directionsDisplay.setMap(map);
}
var directionsService = new google.maps.DirectionsService();
function calcRoute() {
var start = document.getElementById("start").value;
var end = document.getElementById("end").value;
var distanceInput = document.getElementById("distance");
var request = {
origin:start,
destination:end,
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;
var cost=distanceInput.value*2-(distanceInput.value*2*0.20)
var costDiv = document.getElementById("cost");
costDiv.style.display = "";
costDiv.innerHTML = "Cost: "+cost + "<br>";
}
});
}
</script>
<title>Distance Calculator</title>
<style type="text/css">
body {
font-family:Helvetica, Arial;
}
#map_canvas {
height: 50%;
}
</style>
</head>
<body>
<body onload="initialize()">
<p>Enter your current location and desired destination to get the distance</p>
<div>
<p>
<label for="start">Start: </label>
<input type="text" name="start" id="start" />
<br>
<label for="end">End: </label>
<input type="text" name="end" id="end" />
<input type="hidden" name="distance" id="distance" >
<input type="submit" value="Calculate Route" onclick="calcRoute()" />
<div style="display:none;" id="cost"></div>
</p>
<p>
</p>
</div>
<div id="map_canvas"></div>
</body>
</html>
编辑:以下是获取最低距离和相关费用的方法。首先,您必须向Google Maps API提出requestRouteAlternatives = true的请求,否则您将只获得一条路由作为响应。其次,我确实认为谷歌正在按照它们的距离按升序返回路线 - 不完全确定,但你可以假设第一条路线是最小距离的路线。无论如何,这是一种做你想做的事情。
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
var directionDisplay;
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var copenhagen = new google.maps.LatLng(55.6771, 12.5704);
var myOptions = {
zoom:12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: copenhagen
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
directionsDisplay.setMap(map);
}
var directionsService = new google.maps.DirectionsService();
function calcRoute() {
var start = document.getElementById("start").value;
var end = document.getElementById("end").value;
var distanceInput = document.getElementById("distance");
var request = {
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.DRIVING,
provideRouteAlternatives: true
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
if(response.routes[0] != undefined && response.routes[0].legs[0] != undefined) {
/* Take the min distance route */
/ * Assume the first distance is the shortest */
var lowestDistance = response.routes[0].legs[0].distance.value;
for(var i =0; i<response.routes.length;i=i+1) {
console.log(response.routes[i].legs[0].distance.value);
if(lowestDistance <= response.routes[i].legs[0].distance.value) {
lowestDistance = response.routes[i].legs[0].distance.value;
directionsDisplay.setDirections(response);
distanceInput.value = response.routes[i].legs[0].distance.value / 1000;
var cost=distanceInput.value*2-(distanceInput.value*2*0.20)
var costDiv = document.getElementById("cost");
costDiv.style.display = "";
costDiv.innerHTML = "Cost: "+cost + "<br>";
}
}
}
}
});
}
</script>
<title>Distance Calculator</title>
<style type="text/css">
body {
font-family:Helvetica, Arial;
}
#map_canvas {
height: 50%;
}
</style>
</head>
<body>
<body onload="initialize()">
<p>Enter your current location and desired destination to get the distance</p>
<div>
<p>
<label for="start">Start: </label>
<input type="text" name="start" id="start" />
<br>
<label for="end">End: </label>
<input type="text" name="end" id="end" />
<input type="hidden" name="distance" id="distance" >
<input type="submit" value="Calculate Route" onclick="calcRoute()" />
<div style="display:none;" id="cost"></div>
</p>
<p>
</p>
</div>
<div id="map_canvas"></div>
</body>
</html>