谷歌地图方向服务没有显示路线

时间:2015-06-19 17:18:44

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

我使用了谷歌地图api提供的简单方向服务api示例,而地图似乎调整为显示两个不同点,没有路线或标记绘制。

directions-simple

这就是我所拥有的:

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

function initialize(data) {
          directionsDisplay = new google.maps.DirectionsRenderer();
          var chicago = new google.maps.LatLng(41.850033, -87.6500523);
          var mapOptions = {
            zoom:7,
            center: chicago
          };
          map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
          directionsDisplay.setMap(map);

        }

        function calcRoute() {
          var start = "chicago, il";
          var end = "st louis, mo";
          var request = {
              origin:start,
              destination:end,
              travelMode: google.maps.TravelMode.DRIVING
          };
          directionsService.route(request, function(response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
              directionsDisplay.setDirections(response);
            }
          });
        }


<div style="height: 25rem; width: 40rem;" id="map-canvas"></div>

我的代码中缺少什么来显示默认标记和路线?

非常感谢

1 个答案:

答案 0 :(得分:-1)

你必须在初始化之后的某个地方调用calcRoute()。你可以在初始化结束时执行此操作();例如:

    <!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Directions service</title>
    <style>
        html, body, #map-canvas {
            height: 100%;
            margin: 0px;
            padding: 0px
        }

        #panel {
            position: absolute;
            top: 5px;
            left: 50%;
            margin-left: -180px;
            z-index: 5;
            background-color: #fff;
            padding: 5px;
            border: 1px solid #999;
        }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
    <script>
        var directionsDisplay;
        var directionsService = new google.maps.DirectionsService();
        var map;

        function initialize() {
            directionsDisplay = new google.maps.DirectionsRenderer();
            var chicago = new google.maps.LatLng(41.850033, -87.6500523);
            var mapOptions = {
                zoom: 7,
                center: chicago
            };
            map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
            directionsDisplay.setMap(map);
            calcRoute();
        }

        function calcRoute() {
            var start = "chicago, il";
            var end = "st louis, mo";
            var request = {
                origin:start,
                destination:end,
                travelMode: google.maps.TravelMode.DRIVING
            };
            directionsService.route(request, function(response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    directionsDisplay.setDirections(response);
                }
            });
        }


        google.maps.event.addDomListener(window, 'load', initialize);

    </script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>