javascript - 谷歌地图计算文本框更改按钮单击的不同距离

标签 javascript html google-maps google-maps-api-3

这是我面临的非常奇怪的问题。在我的 html 页面中有 2 个与谷歌地图连接的文本框。问题是单击按钮时它会计算出完美的结果,但如果我尝试在文本框更改上运行相同的函数,则会显示错误的结果。这是我的页面的链接。当第二个文本框更改时是否无法获得结果?

代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
    </style>
</head>
<body onload="GetRoute();">
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
    <script type="text/javascript">
        var source, destination;
        var directionsDisplay;
        var directionsService = new google.maps.DirectionsService();
        google.maps.event.addDomListener(window, 'load', function () {
            new google.maps.places.SearchBox(document.getElementById('txtSource'));
            new google.maps.places.SearchBox(document.getElementById('txtDestination'));
            directionsDisplay = new google.maps.DirectionsRenderer({ 'draggable': true });
        });

        function GetRoute() {
            var mumbai = new google.maps.LatLng(18.9750, 72.8258);
            var mapOptions = {
                zoom: 7,
                center: mumbai
            };
            map = new google.maps.Map(document.getElementById('dvMap'), mapOptions);
            directionsDisplay.setMap(map);
            directionsDisplay.setPanel(document.getElementById('dvPanel'));

            //*********DIRECTIONS AND ROUTE**********************//
            source = document.getElementById("txtSource").value;
            destination = document.getElementById("txtDestination").value;

            var request = {
                origin: source,
                destination: destination,
                travelMode: google.maps.TravelMode.DRIVING
            };
            directionsService.route(request, function (response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    directionsDisplay.setDirections(response);
                }
            });

            //*********DISTANCE AND DURATION**********************//
            var service = new google.maps.DistanceMatrixService();
            service.getDistanceMatrix({
                origins: [source],
                destinations: [destination],
                travelMode: google.maps.TravelMode.DRIVING,
                unitSystem: google.maps.UnitSystem.METRIC,
                avoidHighways: false,
                avoidTolls: false
            }, function (response, status) {
                if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {
                    var distance = response.rows[0].elements[0].distance.text;
                    var duration = response.rows[0].elements[0].duration.text;
                    var dvDistance = document.getElementById("dvDistance");
                    dvDistance.innerHTML = "";
                    dvDistance.innerHTML += "Distance: " + distance + "<br />";
                    dvDistance.innerHTML += "Duration:" + duration;

                } else {
                    alert("Unable to find the distance via road.");
                }
            });
        }
    </script>
    <table border="0" cellpadding="0" cellspacing="3">
        <tr>
            <td colspan="2">
                Source:
                <input type="text" id="txtSource" style="width: 200px" />
                &nbsp; Destination:
                <input type="text" id="txtDestination"  onchange="GetRoute(this);"  style="width: 200px" />
                <br />
                <input type="button" value="Get Route" onclick="GetRoute()" />
                <hr />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <div id="dvDistance">
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div id="dvMap" style="width: 500px; height: 500px">
                </div>
            </td>
            <td>
                <div id="dvPanel" style="width: 500px; height: 500px">
                </div>
            </td>
        </tr>
    </table>
    <br />
</body>
</html>

最佳答案

因为您尝试在文本框值更改的同时运行脚本,所以无法获得准确的距离。只需设置定时器,延迟函数运行 2 秒。对此行进行更改。

<input type="text" id="txtDestination"  onchange="setTimeout(function () {GetRoute()}, 2000);"  style="width: 200px" />

它将延迟你的函数 2 秒然后运行。

希望这对你有帮助..

关于javascript - 谷歌地图计算文本框更改按钮单击的不同距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34101509/

相关文章:

javascript - 无法通过谷歌javascript map api获取 map ,不会抛出错误,但iframe src约为:blank

Javascript url 重定向,路径中带有磅符号

javascript - 如何使用 require.js 加载 jQuery with noConflict

javascript - HTML5 Canvas - 绘制一条越来越粗到最后的线的最简单方法是什么?

javascript - 使用 Javascript API 根据多个邮政编码标记 Google map 位置

JavaScript 使用自定义属性作为持久化机制

python - 错误 404 - 使样式表在 Bottle 中工作(Python Web 框架)

html - FlexSlider:从显示图像更改为显示文本的方法?

javascript - Tizen Web 不支持 CDN?

javascript - 为谷歌地图 v3 JS Latlng 对象转换 DATUM WGS84 坐标