0

I am implementing google.maps.DistanceMatrixService()

When I test I am getting a weird anomaly and it is not consistent. As I am driving towards to destination I am hitting a button that checks my distance. each time I hit the button it shows that my distance is getting closer (shorter) then out of the blue the distance jumps up (clearly not an accurate reading) as soon as I hit the button again it is back to normal.

Does anyone have any insight to this anomaly or issue?

here is my code. I am using Vue.js

googleMapsMatrix(lat1, lon1, lat2, lon2) {

    let origin = new google.maps.LatLng(lat2, lon2);
    let destination = new google.maps.LatLng(lat1, lon1);
    let service = new google.maps.DistanceMatrixService();

    service.getDistanceMatrix({
        origins: [origin],
        destinations: [destination],
        travelMode: 'DRIVING',
        // transitOptions: TransitOptions,
        // drivingOptions: DrivingOptions,
        unitSystem: google.maps.UnitSystem.IMPERIAL,
        // avoidHighways: Boolean,
        // avoidTolls: Boolean,
    }, (response, status) => (this.googleMatrixCallback(response, status, lat2, lon2) ));

},
googleMatrixCallback(response, status, lat2, lon2) {
    console.log(response, status, lat2, lon2)
} 
Eindbaas
  • 945
  • 1
  • 8
  • 16
Jason
  • 1,091
  • 4
  • 19
  • 40
  • 1
    Aren't you requesting travel-directions instead of distance? The might change suddenly depending on where you are exactly. – Eindbaas Feb 19 '19 at 16:15
  • I thought I was requesting distance. Am I not? – Jason Feb 19 '19 at 16:16
  • 1
    distance would be to just draw a straight line between your origin and destination, but i am not sure if that is what you want. getDistanceMatrix requests travel directions (you even supply a travelMode in the request) that will not necessarily decrease all the time (if you leave the suggested route, you get a new and different calculated route). (see https://developers.google.com/maps/documentation/javascript/distancematrix) – Eindbaas Feb 19 '19 at 16:41
  • ah yes now I understand what you were referring to. I was using As The Crow Flies for my calculations but I figured using getDistanceMatrix would allow for a more accurate reading of distance from the origin to the destination. And to help, I did not change routes. – Jason Feb 19 '19 at 16:43
  • Are you recommending I not use getDistanceMatrix to calculating distance but instead go back to using "As The Crow Flies" – Jason Feb 19 '19 at 16:51
  • 1
    i don't recommend anything, it depends on what you want. if you need to know how long a straight line between two locations is, you can easily calculate that without doing a request (use google.maps.geometry.spherical.computeDistanceBetween). if you want the travel-distance (which takes in account different types of roads, traffic jams, etc), you need to do the request like you do now. – Eindbaas Feb 19 '19 at 17:22
  • 1) Why use the DistanceMatrix if you are only checking 1 origin and destination? Matrix is for when you need multiple origins and destinations. 2) If you are following a route provided by the Directions API, you could instead calculate the distance from your current position to the destination, on the directions Polyline. See [this example](http://jsfiddle.net/upsidown/ucny147m/) for finding distance from origin to any point on a Polyline. – MrUpsidown Feb 20 '19 at 00:40

0 Answers0