如何查明所要求的区域是否有交通信息?

时间:2014-10-06 09:37:42

标签: javascript google-maps-api-3

我正在使用Google API V3和JavaScript,并希望计算由流量引起的从一个点到另一个点的延迟。

所以,我提出了两个请求(也见下面的代码):一个durationInTraffic = true和一个durationInTraffic = false奇怪的是,两个都返回相同的值(我测试了几个更大的区域,如慕尼黑,柏林,伦敦,西德尼)。现在我想知道我的代码是错误的还是在请求的区域中根本没有可用的流量数据。

function getRoute(){
   var directionsService = new google.maps.DirectionsService();

   var myOptions = {
     zoom:7,
     mapTypeId: google.maps.MapTypeId.ROADMAP
   };


    var startValue=document.getElementById('start').value;
    var zielValue=document.getElementById('ziel').value;

        var stundeValue=document.getElementById('stunde').value;
    var minuteValue=document.getElementById('minute').value;

    var tagValue=document.getElementById('tag').value;
    var monatValue=document.getElementById('monat').value;
    var jahrValue=document.getElementById('jahr').value;

//departure time in Epoch time
    var abfahrtsZeitValue=monatValue+"/"+tagValue+"/"+jahrValue+" "+stundeValue+":"+minuteValue;

        var request_withTraffic = {
            origin: startValue, 
            destination: zielValue,
            travelMode: google.maps.DirectionsTravelMode.DRIVING,
            transitOptions: {
                     departureTime: new Date(abfahrtsZeitValue)
                     },
             durationInTraffic: true
        };

        var request_withoutTraffic = {
            origin: startValue, 
            destination: zielValue,
            travelMode: google.maps.DirectionsTravelMode.DRIVING,
            transitOptions: {
                     departureTime: new Date(abfahrtsZeitValue)
                     },
             durationInTraffic: false
        };

   directionsService.route(request_withTraffic, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {

         // Display the duration:
            document.getElementById('durationWithTraffic').innerHTML="Dauer mit Vekehr: "; 
            document.getElementById('durationWithTraffic').innerHTML += 
            response.routes[0].legs[0].duration.value + " seconds";            
      }
   });

   directionsService.route(request_withoutTraffic, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) 
        {   
            document.getElementById('durationWithoutTraffic').innerHTML="Dauer ohne Verkehr: "; 
            document.getElementById('durationWithoutTraffic').innerHTML += 
            response.routes[0].legs[0].duration.value + " seconds";            
        }
   });
}

0 个答案:

没有答案