Kévin Vilela Pinto Kévin Vilela Pinto - 1 month ago 10
Javascript Question

Probleme with Google Direction Matrix API

I tried to find the duration_in_traffic value from API.

When I use the the Distance Matrix Service object like this :

service.getDistanceMatrix(
{
origins : i === 0 ? [origin] : [destination],
destinations : i === 0 ? [destination] : [origin],
travelMode : google.maps.TravelMode.DRIVING,
drivingOptions : {
departureTime : departureTime[i],
trafficModel : google.maps.TrafficModel.PESSIMISTIC
}
}, callbackCar);


The result don't contains duration_in_traffic field.




When I use instead the GET API service with JSONP the console contains an Unexpected token : error ...

getDelay: function() {
return $resource('https://maps.googleapis.com/maps/api/distancematrix/json', {
key: ACCESS.ggAPI,
origins: "@origins",
destinations: "@destinations",
language: "fr-FR",
mode: "@mode",
transit_mode: "@transitMode",
traffic_model: "@traffic_model"
}, {
get: {
method: 'JSONP'
}
});
}


When I try to use GET instead JSONP, I have a CORS issue...

When I try to put the uri built by the angular service in a other window, i can find the correct result :

{
"destination_addresses" : [ "93200 Saint-Denis, France" ],
"origin_addresses" : [ "95500 Gonesse, France" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "16,7 km",
"value" : 16746
},
"duration" : {
"text" : "19 minutes",
"value" : 1133
},
**"duration_in_traffic" : {
"text" : "23 minutes",
"value" : 1391
},**
"status" : "OK"
}
]
}
],
"status" : "OK"
}


What can I do ?

Answer

Without a Premium Plan you cannot get the duration_in_traffic attribute.

The total duration of this leg, taking into account the traffic conditions indicated by the trafficModel property. This property may be undefined as the duration may be unknown. Only available to Premium Plan customers when drivingOptions is defined when making the request.

Source

Comments