0

为简单起见,我将用于显示地图和路线的代码放在函数“ initialize ”中。地图正在显示,但路线未显示。请指出错误。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google Maps JavaScript API Example</title>
    <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>

    <script type="text/javascript"> 

        var map;
        var latlng = new google.maps.LatLng (-34.397, 150.644);

        var directionsDisplay = new google.maps.DirectionsRenderer ();;
        var directionsService = new google.maps.DirectionsService ();

        function initialize ()
        {
            var myOptions = 
            {
                zoom:8,
                center:latlng,
                mapTypeId:google.maps.MapTypeId.ROADMAP
            };

            map = new google.maps.Map (document.getElementById ("map"), myOptions);

            directionsDisplay.setMap (map);

            var initialPoint             = latlng;

            var latlng2 = new google.maps.LatLng (-34.400, 150.744);
            var pointAfterDragging       = latlng2;

            var start = initialPoint;
            var end   = pointAfterDragging;

            var request = {
                        origin:start,
                        destination:end,
                        travelMode:google.maps.TravelMode.DRIVING
                        };


            directionsService.route (request, 
                                function (result, status) 
                                {
                                    if (status == google.maps.DirectionsStatus.OK)
                                    {
                                        directionsDisplay.setDirections (result);
                                    }
                                });
            }

        </script> 
    </head>
    <body onload="initialize()" onunload="GUnload()" topmargin="0" leftmargin="0">
    <div id="map" style="width: 341px; height: 271px"></div>
    <div id="directionsPanel" style="float:right;width:30%;height 100%"></div>
    </body>
    </html> 
4

1 回答 1

1

你有两个问题。

首先没有定义称为 GUnload 的方法。

其次,您尝试获取的方向返回 ZERO_RESULTS。您可以随时尝试提醒状态以查看它是否返回任何方向。如果没有,则不会显示任何内容。

您应该尝试使用其他一些位置,它应该没问题。如果你想尝试一下,你也可以传递它的地址而不是 latlng 的:)

要提醒状态,您可以这样做:

directionsService.route (request, 
                            function (result, status) 
                            {
                                alert(status);
                                if (status == google.maps.DirectionsStatus.OK)
                                {

                                    directionsDisplay.setDirections (result);
                                }
                            });
        }
于 2011-05-31T08:54:09.213 回答