2016-01-20 132 views
0

我試圖設置一個departureTime選項,但似乎不起作用。在本例中博爾米奧和普拉託阿洛斯泰爾維奧之間的道路ss38被關閉。 從8月份開始,我預計你正在使用這條路,而不是目前通過瑞士提供的路。Google maps api departureTime in directionsService

感謝

這裏是我的代碼:

function initialize() { 

    map = new google.maps.Map(document.getElementById("map"), { 
    mapTypeId: google.maps.MapTypeId.ROADMAP, 
    mapTypeControlOptions: { 
     style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR, 
     position: google.maps.ControlPosition.TOP_CENTER 
    }, 
    streetViewControl: true, 
    streetViewControlOptions: { 
     position: google.maps.ControlPosition.TOP_LEFT 
    }, 
    zoomControl: true, 
    zoomControlOptions: { 
     position: google.maps.ControlPosition.LEFT_TOP 
    } 

    }); 
    map.setZoom(10); // This will trigger a zoom_changed on the map 
    map.setCenter(new google.maps.LatLng(46.6199, 10.5924)); 


    directionsDisplay.setMap(map); 
    geocoderService = new google.maps.Geocoder(); 
    directionsService = new google.maps.DirectionsService; 

    var marker = new google.maps.Marker({ 
    position: new google.maps.LatLng(46.6199, 10.5924), 
    map: map, 
    draggable: true 
    }); 
    var marker2 = new google.maps.Marker({ 
    position: new google.maps.LatLng(46.4693, 10.3731), 
    map: map, 
    draggable: true 
    }); 

    calcolapercorso(tipodipercorso); 

} 



function calcolapercorso(tipodipercorso) { 

    var request = { 
    origin: new google.maps.LatLng(46.6199, 10.5924), 
    destination: new google.maps.LatLng(46.4693, 10.3731), 
    optimizeWaypoints: false, 
    avoidHighways: true, 
    region: "IT", 


    travelMode: google.maps.TravelMode.DRIVING, 
    drivingOptions: { 
     departureTime: new Date('2016-08-11T00:00:00'), 
     trafficModel: google.maps.TrafficModel.PESSIMISTIC 
    } 

    }; 
    //request.travelMode = google.maps.DirectionsTravelMode.DRIVING; 
    request.unitSystem = google.maps.UnitSystem.METRIC; 

    directionsService.route(request, function(response, status) { 

    if (status == google.maps.DirectionsStatus.OK) { 

     var polyLine = { 
     strokeColor: "#2B8B3F", 
     strokeOpacity: 1, 
     strokeWeight: 4, 
     }; 

     directionsDisplay.setOptions({ 
     polylineOptions: polyLine, 
     suppressMarkers: true 
     }); 


     directionsDisplay.setDirections(response); 

    } else if (status == google.maps.DirectionsStatus.ZERO_RESULTS) { 
     alert("Could not find a route between these points"); 
    } else { 
     alert("Directions request failed"); 
    } 
    }); 
} 
+2

請格式化你的代碼,併爲你的意思時,你說這是行不通的就添加更多的細節。你在期待什麼,會發生什麼? – phoenix

+0

@Fillo,爲了使用departureTime,您必須使用Google Maps API的客戶端ID才能工作(我想您已經擁有了它)。其實,我感覺和你一樣,即使在美國,我也會向Google Maps API團隊要求。 您的代碼至少是正確的。 – wf9a5m75

回答

0

我想通了,當你設置的任何航點(至少在谷歌地圖的JavaScript API的departureTime選項不工作,我沒有確認RESTAPI )。 沒有航點,它的工作。

enter image description here

<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="utf-8" /> 
    <style type="text/css"> 
    #map_canvas { 
     width: 600px; 
     height: 400px; 
     border: 1px solid gray; 
     float: left; 
    } 
    #direction_panel { 
     width: 250px; 
     height: 400px; 
     border: 1px solid gray; 
     overflow-y:scroll; 
    } 
    </style> 
    <script src="https://maps.googleapis.com/maps/api/js?language=en&v=3.exp&client=[YOUR_CLIENT_ID]"></script> 
    <script> 
     function initialize() { 
     var mapDiv = document.getElementById("map_canvas"); 
     var map = new google.maps.Map(mapDiv, { 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }); 

     var form = document.getElementById("form"); 
     google.maps.event.addDomListener(form, "submit", function(event) { 
      if (event.preventDefault) { 
      event.preventDefault(); 
      } else { 
      event.cancelBubble = true; 
      event.returnValue = false; 
      } 
      map.set("traffic", google.maps.TrafficModel[this.trafficModel.value]); 
     }); 

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

     var panelDiv = document.getElementById("direction_panel"); 

     var directionsRenderer = new google.maps.DirectionsRenderer({ 
      map: map, 
      panel: panelDiv 
     }); 
     map.set("directionsRenderer", directionsRenderer); 

     map.addListener("traffic_changed", onTrafficModelChanged); 
     } 

     function onTrafficModelChanged() { 
     var map = this; 
     var departureDateTime = document.getElementById("departureTime").value; 
     var directionsService = map.get("directionsService"); 
     var directionsRenderer = map.get("directionsRenderer"); 
     var trafficModel = map.get("traffic"); 

     var request = { 
      origin: "<YOUR ORIGIN>", 
      destination: "<YOUR DESTINATION>", 
      travelMode: google.maps.TravelMode.DRIVING, 
      drivingOptions: { 
      departureTime: new Date(departureDateTime), 
      trafficModel: trafficModel 
      } 
     }; 

     directionsService.route(request, function(result, status) { 
      if (status != google.maps.DirectionsStatus.OK) { 
      alert(status); 
      return; 
      } 
      directionsRenderer.setDirections(result); 
     }); 
     } 

     google.maps.event.addDomListener(window, "load", initialize); 
    </script> 
    </head> 
    <body> 
    <div id="frame"> 
     <div id="map_canvas"></div> 
     <div id="direction_panel"></div> 
    </div> 
    <form id="form"> 
    Departure Time: <input type="text" id="departureTime" size=40 value="2016/01/25 21:00 PST"><br> 
     <input type="radio" name="trafficModel" value="OPTIMISTIC">Optimistic 
     <input type="radio" name="trafficModel" value="BEST_GUESS" checked>BEST_GUESS 
     <input type="radio" name="trafficModel" value="PESSIMISTIC">PESSIMISTIC 
     <input type="submit" value="Search"> 
    </form> 
    </body> 
</html> 
+0

更新:在日本似乎離開時間不起作用。 – wf9a5m75