directionsService.route没有通过?

时间:2016-02-07 17:56:03

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

我正在尝试使用Google Maps Javascript API,我已经复制了教程和路线代码,但我无法使DirectionsService.route工作。这是我的代码。

var directionsDisplay;
var directionsService;
var map;

function initialize() {
    directionsService = new google.maps.DirectionsService();
    directionsDisplay = new google.maps.DirectionsRenderer({map:map});

    var chicago = new google.maps.LatLng(41.850033, -87.6500523);
    var mapOptions = {
        zoom:9,
        center: chicago
    }
    map = new google.maps.Map(document.getElementById("map"), mapOptions);
    directionsDisplay.setMap(map);
    console.log(google.maps);
    console.log(directionsDisplay);
    console.log(directionsService);
    console.log(directionsService.route);
}

$(document).ready(function() {
    function calcRoute() {
        // console.log(map);
        var start = document.getElementById("origin").value;
        var end = document.getElementById("destination").value;
        console.log('start');
        console.log(start);
        console.log('end');
        console.log(end);
        var request = {
            origin:start,
            destination:end,
            travelMode: google.maps.TravelMode.DRIVING
        };
        console.log(request);
        console.log(google.maps);
        console.log(google.maps.DirectionsService);
        console.log(directionsDisplay);
        console.log(directionsService);
        console.log(directionsService.route);
        directionsService.route(request, function (result, status) {
            console.log('hi');
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(result);
            }
        });
    };

    $('#origin-search').submit(function() {
        console.log('calling calcRoute');
        calcRoute();
    });
});

永远不会访问console.log('hi')语句。附件是控制台的日志语句。我首先加载索引,然后执行$('#origin-search')。submit(),然后重新路由回加载索引。请注意,directionsService.route函数在initialize()内部的记录方式与calcRoute()内部的记录方式不同。

Log Statements

1 个答案:

答案 0 :(得分:2)

如果我将calcRoute移动到$(document).ready,我会收到错误,因为地图尚未初始化。只有在确定地图已初始化后,才会调用calcRoute。

以下是一些有效的代码:

<!DOCTYPE html>
<html>

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

<body>
  <div id="map"></div>
  <script>
    var directionsDisplay;
    var directionsService;
    var map;

    function initMap() {
      directionsService = new google.maps.DirectionsService;
      var chicago = new google.maps.LatLng(41.850033, -87.6500523);
      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 8,
        center: chicago
      });
      directionsDisplay = new google.maps.DirectionsRenderer({
        map: map
      });
      calcRoute();
    }

    function calcRoute() {
      var start = "cambridge, ma";
      var end = "boston, ma";
      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>
  <script src="https://maps.googleapis.com/maps/api/js?callback=initMap" async defer></script>
</body>

</html>