Reputation: 13
I am trying to get travel time with traffic between two locations. I have followed the documentation guide with all details, but I always get a fixed traveltime between two points regardless to trafficmodel: (best_guess, pessimistic or optimistic). Also, I tried to change the value of departure time to different dates in future but I always get the same result.
Edit:
I am using a standard API key. Also, I have tried to change date format for [departureTime:] but did not help.
My main goal is to get travel time on links with traffic.
Thanks for helping.
Here is my HTML and Javascriptcode:
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Displaying text directions with <code>setPanel()</code></title>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
#floating-panel {
position: absolute;
top: 10px;
left: 25%;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
text-align: center;
font-family: 'Roboto','sans-serif';
line-height: 30px;
padding-left: 10px;
}
#right-panel {
font-family: 'Roboto','sans-serif';
line-height: 30px;
padding-left: 10px;
}
#right-panel select, #right-panel input {
font-size: 15px;
}
#right-panel select {
width: 100%;
}
#right-panel i {
font-size: 12px;
}
#right-panel {
height: 100%;
float: right;
width: 390px;
overflow: auto;
}
#map {
margin-right: 400px;
}
#floating-panel {
background: #fff;
padding: 5px;
font-size: 14px;
font-family: Arial;
border: 1px solid #ccc;
box-shadow: 0 2px 2px rgba(33, 33, 33, 0.4);
display: none;
}
@media print {
#map {
height: 500px;
margin: 0;
}
#right-panel {
float: none;
width: auto;
}
}
</style>
</head>
<body>
<div id="floating-panel">
<strong>Get TravelTime in Traffic</strong>
</div>
<div id="right-panel"></div>
<div id="map"></div>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=[API_KEY]&callback=initMap">
</script>
<script>
function initMap() {
var directionsDisplay = new google.maps.DirectionsRenderer;
var directionsService = new google.maps.DirectionsService;
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: {lat: 30.114114, lng: 31.420595}
});
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('right-panel'));
var control = document.getElementById('floating-panel');
control.style.display = 'block';
map.controls[google.maps.ControlPosition.TOP_CENTER].push(control);
calculateAndDisplayRoute(directionsService, directionsDisplay);
}
function calculateAndDisplayRoute(directionsService, directionsDisplay) {
directionsService.route({
origin: "11 The Ring Road, Al Khosous, Al Khankah, Al Qalyubia Governorate, Egypt",
destination: " El-Shams Sporting Club, Al Matar, Qism El-Nozha, Cairo Governorate, Egypt",
travelMode: google.maps.TravelMode.DRIVING,
drivingOptions: {
departureTime: new Date("June 14, 2016 11:13:00"),
trafficModel: google.maps.TrafficModel.PESSIMISTIC
}
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
</script>
</body>
</html>
Upvotes: 1
Views: 740
Reputation: 17613
It seems this has nothing to do with your code. Instead, it is a matter of licensing. Your are using Driving Options (BEST_GUESS, OPTIMISTIC,PESSIMISTIC) which is a feature that belongs to Premium Plan clients. The way I see it, you need to buy the license first and use the Premium clientID before you can use this.
"You must supply a Google Maps APIs Premium Plan client ID when loading the API if you want to include a drivingOptions field in the DistanceMatrixRequest."
Your client ID
Upon purchasing your Google Maps APIs Premium Plan license, you will receive a welcome email from Google that contains your client ID. Your client ID is used to access the special features of Google Maps APIs Premium Plan. All client IDs begin with a gme- prefix.
This client ID is not a key. It will only work from URLs which you authorize, so you don't need to worry about keeping it secret.
Upvotes: 1
Reputation: 89
Maybe your date format is not correct ? Try :
departureTime: new Date("2016-06-14T11:13:00");
Upvotes: 0