googlemaps / google-maps-services-js

Node.js client library for Google Maps API Web Services
Apache License 2.0
2.91k stars 640 forks source link

Distance Matrix 'duration_in_traffic' value is not accurate using the 'best_guess' traffic model #97

Closed qbunt closed 7 years ago

qbunt commented 7 years ago

Using the following code, the durations returning back from the Distance Matrix API have incorrect duration and duration_in_traffic values with the same origin and destination values:

var requestETA = (from, to)=>{
    var requestObj = {
        origins: [from],
        destinations: [to],
        units: 'imperial',
        mode: 'driving',
        departure_time: 'now',
        traffic_model: 'best_guess'
    }

    return new Promise((resolve, reject) => {
        googleMapsClient.distanceMatrix(requestObj, (err, response) => {
            if(err){
                reject(err)
            } else {
                 console.log(response.json.rows[0].elements[0].duration_in_traffic.text)
                resolve(response.json.rows[0].elements[0].value);
            }
        })
    })
}

returns a value of 50 mins on the relevant element row.

Running the exact same parameters in the Go client for the same API returns a much more accurate result (identical to http://maps.google.com). Using the same basic implementation, I get a value for duration_in_traffic of 1h3m22s.

Happy to send the Go implementation of the same thing, I was simply surprised that this library appeared to be returning the wrong value. Let me know if you'd like additional info.

stephenmcd commented 7 years ago

You shouldn't expect identical queries run at different times to produce the same values. The Go and JS clients only differ in that the Go client converts the returned value to a time.Duration, whereas the JS client returns the raw JSON from the directions API.

Here's output from my shell using both clients. You'll see responses vary from request to request within the JS client, and also vary from request to request with the Go client:

~ googlemaps directions --origin Sydney --destination Melbourne --departure_time now --units imperial --mode driving --traffic_model best_guess | grep -A 3 duration_in_traffic
                    "duration_in_traffic": {
                        "text": "9 hours 8 mins",
                        "value": 32870
                    },
~ googlemaps directions --origin Sydney --destination Melbourne --departure_time now --units imperial --mode driving --traffic_model best_guess | grep -A 3 duration_in_traffic
                    "duration_in_traffic": {
                        "text": "9 hours 8 mins",
                        "value": 32884
                    },
~ googlemaps directions --origin Sydney --destination Melbourne --departure_time now --units imperial --mode driving --traffic_model best_guess | grep -A 3 duration_in_traffic
                    "duration_in_traffic": {
                        "text": "9 hours 8 mins",
                        "value": 32881
                    },
~ googlemaps directions --origin Sydney --destination Melbourne --departure_time now --units imperial --mode driving --traffic_model best_guess | grep -A 3 duration_in_traffic
                    "duration_in_traffic": {
                        "text": "9 hours 8 mins",
                        "value": 32880
                    },
~ go run directions/cmdline/main.go -key API_KEY -origin Sydney -destination Melbourne -departure_time now -units imperial -mode driving -traffic_model best_guess | grep DurationInTraffic
                DurationInTraffic: 32873000000000,
~ go run directions/cmdline/main.go -key API_KEY -origin Sydney -destination Melbourne -departure_time now -units imperial -mode driving -traffic_model best_guess | grep DurationInTraffic
                DurationInTraffic: 32891000000000,
~ go run directions/cmdline/main.go -key API_KEY -origin Sydney -destination Melbourne -departure_time now -units imperial -mode driving -traffic_model best_guess | grep DurationInTraffic
                DurationInTraffic: 32871000000000,

If you're truly convinced the different clients are performing different requests, I would attempt to add some logging to the source for both clients, showing the URLs they're both using - I suspect they'll be identical.