0

我可以在地图上选择两个点来计算距离,然后使用下面的代码查看这两个点之间的路径。但是当我在地图上选择第三个点时,我无法计算第三个点到第二个点的距离,或者我在地图上看不到 3 和 2 之间的路。有人可以帮帮我吗?

非常感谢。

编码:

<!DOCTYPE html>
<html>
<head>
<script src="http://maps.google.com/maps/api/js?sensor=false">

</script>
<body>

<script type="text/javascript">
        var map;
        var split = true;
        var directionsService;
        var directionsRenderer;
        var markersArray = [];

        function initialize() {
            map = new google.maps.Map(document.getElementById("map_canvas"), {
                zoom : 7,
                mapTypeId : google.maps.MapTypeId.ROADMAP,
                center : new google.maps.LatLng(-33.868011, -151207566)
            });

            map = new google.maps.Map(document.getElementById("map_canvas"),
                    initialize);
            google.maps.event.addListener(map, 'click', function(event) {
                addMarker(event.latLng);
            });

            function addMarker(location) {
                marker = new google.maps.Marker({
                    position : location,
                    map : map
                });
                markersArray.push(marker);
            }

            directionsRenderer = new google.maps.DirectionsRenderer();
            directionsRenderer.setMap(map);
            directionsRenderer.setPanel(document.getElementById('directions'));

            directionsService = new google.maps.DirectionsService();

            Form = document.forms['harita'];
            YolTarifi(Form.elements['nerden'].value,
                    Form.elements['nereye'].value);

        }

        function YolTarifi(Nerden, Nereye) {
            var request = {
                origin : Nerden,
                destination : Nereye,
                travelMode : google.maps.DirectionsTravelMode.DRIVING,
                unitSystem : google.maps.DirectionsUnitSystem.METRIC
            };

            directionsService.route(request, function(response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    directionsRenderer.setDirections(response);
                } else {
                    alert('Yol Tarifi Oluşturulamadı');
                }
            });
        }
    </script>
</body>
</head>
<body onload="initialize()">
    <form name="harita" action="#">
        <table>
            <tr>
                <th align="right">Nerden :</th>
                <td><input type="text" size="25" name="nerden" value="Ankara" /></td>
                <th align="right">Nereye</th>
                <td align="right"><input type="text" size="25" name="nereye"
                    value="İstanbul" /></td>
                <th><input name="submit" type="button"
                    value="Yol Haritasını Göster"
                    onclick="YolTarifi(document.forms['harita'].elements['nerden'].value, document.forms['harita'].elements['nereye'].value)" />
                </th>
            </tr>
        </table>
    </form>
    <br />
    <table class="directions">
        <tr>
            <td valign="top"><div id="directions" style="width: 275px"></div></td>
            <td valign="top"><div id="map_canvas"
                    style="width: 800px; height: 500px"></div></td>
        </tr>
    </table> 
</body>
</html>
4

0 回答 0