Google Map API V3。无法为TRANSIT路线创建自定义折线

时间:2013-10-29 21:36:54

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

我无法在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);
      }
    });
  }

1 个答案:

答案 0 :(得分: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);
  }
});

updated fiddle