1

我无法在 Google API 上为公交路线创建自定义折线。仅渲染部分路线,而不是全部。它适用于驾驶、步行和骑自行车,但不适用于过境。不知道我到底错过了什么。如果有人遇到同样的问题,请帮忙!我在小提琴中做了一个例子:

http://jsfiddle.net/srs/vF2e9/1/

示例代码

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

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

  function calcRoute() {
    var start = "98012";
    var end = "98014";
    var request = {
      origin: start,
      destination: end,
      travelMode: google.maps.TravelMode.TRANSIT
    };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        var polyLine = new google.maps.Polyline({
            strokeColor: '#FF0000'
          });
        var options = {};
        options.directions = response;
        options.map = map;
        options.polylineOptions = polyLine;
        //options.suppressMarkers = true;
        directionsDisplay.setOptions(options);// = new google.maps.DirectionsRenderer(options);
        polyLine.setMap(map);
        //directionsDisplay.setDirections(response);
      }
    });
  }
4

1 回答 1

1

polylineOptions 匿名对象不是(也不应该是)google.maps.Polyline。

directionsService.route(request, function(response, status) {
  if (status == google.maps.DirectionsStatus.OK) {
    var polyLineOptions = {
        strokeColor: '#FF0000'
      };
    var options = {};
    options.directions = response;
    options.map = map;
    options.polylineOptions = polyLineOptions;
    //options.suppressMarkers = true;
    directionsDisplay.setOptions(options);// = new google.maps.DirectionsRenderer(options);
    polyLine.setMap(map);
    //directionsDisplay.setDirections(response);
  }
});

更新的小提琴

于 2013-10-29T22:03:01.107 回答