0

我目前正在研究一个需要使用“Google Transit API V3”集成“Trip Planner”的旅游领域。我已完成设置地图和旅行计划者所需的所有选项。但在这里我面临着问题。

我想在 google API 的“TransitDetails”对象中设置“arrival_time”和“departure_time”。我创建了一个对象,它返回路线上的过境详细信息。但不是按照对象中传递的“出发”和“到达”选项。这就是我在代码中所做的

var directions = new google.maps.DirectionsService();
var renderer = new google.maps.DirectionsRenderer();
var startLocationAutocomplete;
var endLocationAutocomplete;
var map, transitLayer;

function initialize() {
    var mapOptions = {
        zoom:14,
        center:new google.maps.LatLng(51.538551, -0.016633),
        mapTypeId:google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById('map'), mapOptions);

    google.maps.event.addDomListener(document.getElementById('go'), 'click', route);


    var defaultBounds = new google.maps.LatLngBounds(
            new google.maps.LatLng(35.371359, -79.319916),
            new google.maps.LatLng(36.231424, -77.752991));


    var autocompleteOptions = {
        bounds:defaultBounds,
        types:[ "locality", "political", "geocode" ]
    };
    startLocationAutocomplete = new google.maps.places.Autocomplete(document.getElementById('from'));
    endLocationAutocomplete = new google.maps.places.Autocomplete(document.getElementById('to'));

    startLocationAutocomplete.bindTo('bounds', map);
    endLocationAutocomplete.bindTo('bounds', map);

    transitLayer = new google.maps.TransitLayer();

    var control = document.getElementById('transit-wpr');
    map.controls[google.maps.ControlPosition.TOP_RIGHT].push(control);

    google.maps.event.addDomListener(control, 'click', function () {
        transitLayer.setMap(transitLayer.getMap() ? null : map);
    });

    addDepart();
    route();
}

function addDepart() {
    var departHr = document.getElementById('departHr');
    var departMin = document.getElementById('departMin');
    for (var hr = 1; hr < 12; hr++) {
        departHr.innerHTML += '<option value = "'+hr+'">' + hr + '</option>';
    }

    for (var i = 0; i < 12; i++) {
        for (var j = 0; j < 60; j += 5) {
            var x = i < 10 ? '0' + i : i;
            var y = j < 10 ? '0' + j : j;
            departMin.innerHTML += '<option value = "'+y+'">' + y + '</option>';
        }
    }
}

function formatAMPM() {
    var date = new Date();
    var hours = document.getElementById('departHr').value;
    var minutes =  document.getElementById('departMin').value;
    var ampm =  document.getElementById('timeFormat').value;
    /*hours = hours % 12;
    hours = hours ? hours : 12; // the hour '0' should be '12'
    minutes = minutes < 10 ? '0' + minutes : minutes;*/
    console.log(hours);
    if(ampm == 'pm'){
        hours = 12 + parseInt(hours);        }
    var strTime = hours + ':' + minutes;
    return strTime;
}

function route() {
    var startLocation = document.getElementById('from').value;
    var endLocation = document.getElementById('to').value;
    var selectedMode = document.getElementById('modeOfTransportation').value;
    var departure = formatAMPM();
    var bits = departure.split(':');
    var now = new Date();
    var tzOffset = (now.getTimezoneOffset() + 60) * 60 * 1000;

    var time = ($('#travelDate').val() != '') ? new Date($('#travelDate').val()) : new Date();

    time.setHours(bits[0]);
    time.setMinutes(bits[1]);

    var ms = time.getTime() - tzOffset;


    var departureTime = time;


        var request = {
            origin:startLocation,
            destination:endLocation,
            travelMode:google.maps.TravelMode[selectedMode],
            provideRouteAlternatives:true,
            transitOptions:{
                departureTime:departureTime
            }
        };


    console.log(request);
    var panel = document.getElementById('panel');
    panel.innerHTML = '';
    directions.route(request, function (response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            renderer.setDirections(response);
            renderer.setMap(map);
            renderer.setPanel(panel);
            console.log(status);
        } else {
            renderer.setPanel(null);
            alert(status);
        }
    });

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

有人可以通过任何参考或任何代码提示来帮助我纠正它。

谢谢

4

1 回答 1

2

你需要做这样的事情

//TranDep and TranArr have a valid date or ''
if ((TranDep == '') && (TranArr == '')) var Transit = null;
else {
  if ((TranDep != '') && (TranArr != ''))
    var Transit = {arrivalTime: new Date(TranArr), departureTime: new Date(TranDep)}
  else {
    if ((TranDep == '') && (TranArr != ''))
      var Transit = {arrivalTime: new Date(TranArr)}
    else 
      var Transit = {departureTime: new Date(TranDep)}
  }
}

var Dir = new google.maps.DirectionsService();
var request = {
      transitOptions: Transit,
      ......... // the others properties
};

如果您指定了到达时间,则出发时间将被忽略。

问候

于 2013-01-15T23:11:49.380 回答