我有里程計算器的HTML/Javascript代碼,但點擊電子郵件鏈接無法正常工作。當我按下網頁中的鏈接時,電子郵件將正確打開,但是在電子郵件正文中,而不是顯示計算的里程數,表示「未定義」。HTML里程計算器無法正常工作
有人可以告訴我我需要寫的代碼來解決這個問題,並把它放在哪裏。謝謝。
我的代碼如下:
<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" />
<label for="end">End: </label>
<input type="text" name="end" id="end" />
<input type="submit" value="Calculate Route" onclick="calcRoute()" />
</p>
<p>
<label for="distance">Distance (km): </label>
<input type="text" name="distance" id="distance" readonly="true" />
</p>
<a href="mailto:[email protected]?subject=Mileage&body="
onclick="this.href += calcRoute('string', 'distance');"
>
Click to send email
</a>
</div>
<div id="map_canvas"></div>
</body>
</html>
javascript google geolocation maps
calcRoute在哪裏返回值? –
我不知道。人們總是告訴我這是問題,但我不知道如何解決。 –
會在答案中解釋... –