1

这是我的代码:

function trainRoute(startPoint, endPoint) {
    var polyline = new google.maps.Polyline({ strokeColor: '#af1a1a', strokeWeight: 2 });
    var directionsService = new google.maps.DirectionsService();
    var directionsDisplay = new google.maps.DirectionsRenderer({ polylineOptions: polyline, suppressMarkers: true });
    directionsDisplay.setMap(map);

    var request = {
        origin: startPoint,
        destination: endPoint,
        travelMode: google.maps.DirectionsTravelMode.TRANSIT 
    };

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

            var legs = response.routes[0].legs;
            for (i = 0; i < legs.length; i++) {
                var steps = legs[i].steps;
                for (j = 0; j < steps.length; j++) {
                    var nextSegment = steps[j].path;
                    for (k = 0; k < nextSegment.length; k++) {
                        polyline.getPath().push(nextSegment[k]);
                        //bounds.extend(nextSegment[k]);
                    }
                }
            }
        }
    });
}

使用travelMode: google.maps.DirectionsTravelMode.WALKINGI see the line,但这不是我想要的。我想我需要.TRANSIT更多的选择,比如VehicleType.RAIL.

我不知道该怎么做!我试过在谷歌上搜索,但找不到任何东西!


我认为这是正确的方法:

var request = {
    origin: puntoPartenza,
    destination: puntoArrivo,
    travelMode: google.maps.TravelMode.TRANSIT
};

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

        var legs = response.routes[0].legs;
        for (i = 0; i < legs.length; i++) {
            var steps = legs[i].steps;
            for (j = 0; j < steps.length; j++) {
                var transitMode = steps[j].travel_mode;

                if (transitMode == "TRANSIT") {
                    var vehicle = steps[j].transit.line.vehicle.type;
                    if (vehicle == "HEAVY_RAIL") {
                        var nextSegment = steps[j].path;

                        for (k = 0; k < nextSegment.length; k++) {
                            polyline.getPath().push(nextSegment[k]);
                        }                            
                    }
                }


            }
        }
    }
});

但 :

1)为什么它只画了一部分路线?

2) 在 DirectionsStatus 返回的 JSON 对象上,还有关于步行和/或 TRANSIT 的信息。那么travelMode: google.maps.DirectionsTravelMode.TRANSIT?!?! 是什么意思?

但我明白了TypeError: google.maps.TransitVehicle is undefined

4

3 回答 3

2

您不能只编写语法并期望它能够工作Transit VehicleType确实包含 RAIL,但TransitVehicle没有车辆属性。

Transit 车辆中没有财产车辆。查看“类型”属性。

于 2012-11-14T13:48:19.027 回答
1

你可以试试这个,它对我有用,

var directionsServiceRequest = {
                    destination: {lat: 39.79, lng: -86.14},
                    origin: {lat: 41.85, lng: -87.65},
                    travelMode: google.maps.TravelMode.TRANSIT,
                    transitOptions: {modes:['TRAIN']}
          };
于 2016-01-23T13:13:08.837 回答
0

您是否在文档中看到了这一点-

https://developers.google.com/maps/documentation/javascript/reference#TransitVehicle

它可能是 .TRAIN 而不是 .RAIL

于 2012-11-14T13:47:25.423 回答