0

我正在尝试为一个应用程序创建一个里程计算器,我在网上找到了代码。

<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,
   `enter code here` 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>
    </div>
    <div id="map_canvas"></div>
</body>
</html>

javascript google geolocation maps 

我希望在打开用户电子邮件(例如 PC 上的 Windows Live Mail 或平板电脑上的 Yahoo Mail 应用程序)的页面上的链接上有一个点击电子邮件链接,并在电子邮件正文中显示“您的里程是 ???”。我可以这样做,但我希望自动输入里程。我已在脚本的 html 部分中添加了此代码...

<p><a href="mailto:youremail.yahoo.com?subject=Mileage
&body=calcRoute('string', 'distance')">Click to send email</a></p>

...它会打开电子邮件,但不显示计算出的里程。相反,它在正文中显示 calcRoute('string, distance')。我如何让它显示里程?

我知道这个问题很长,但如果您能花时间回答,我将不胜感激。尽快得到有效的答案非常重要。谢谢你。

4

2 回答 2

1

您必须在链接 onclick 事件中计算 mailto 属性值。假设 calcRoute 函数返回一个字符串,下面的代码应该可以工作:

<a href="mailto:youremail.yahoo.com?subject=Mileage&body="
   onclick="this.href += calcRoute('string', 'distance');"
>
Click to send email
</a>
于 2013-07-28T13:45:29.543 回答
0

你应该看看这个链接应该会有所帮助

我的猜测是它没有检测到主题。你确定你的javascriptCalcRoute 返回一个变量吗?

于 2013-07-28T13:23:06.903 回答