0
votes

I want to calculate the exact distance as possible, but currently I just add origin and the destination and let the Google Direction API calculate it, but that doesn't mean it will be the correct route, so I wonder if it's possible to use encoded polyline to calculate the distance with Distance Matrix or something?

Call<Direction> call = RESTClient.getGoogleApiService().getDirectionsWithLatLng(startLatLng, endLatLng);

...

@GET("/maps/api/directions/json")
Call<Direction> getDirectionsWithLatLng(
        @Query(value = "origin") String position,
        @Query(value = "destination") String destination);

What is the different of :

https://maps.googleapis.com/maps/api/directions/json?origin=sydney,au&destination=perth,au

https://maps.googleapis.com/maps/api/directions/json?origin=sydney,au&destination=perth,au&waypoints=enc:lexeF{~wsZejrPjtye@:

1

1 Answers

0
votes

Google Maps Distance Matrix API returns information based on the recommended route between start and end points, as calculated by the Google Maps API. So the results here are only based on the calculated distance by Google. Also, this service does not return detailed route information. Route information can be obtained by passing the desired single origin and destination to the Google Maps Directions API.

So unlike Directions API that you can set the way of what you want to calculate by using the waypoints, Distance Matrix API just calculate distance of the best way or the proper way in where you set the origin and destination.

For your question about the two JSON request.

The first request returns its response without waypoints, So basically it is just like the Distance Matrix API that returns its information based on the recommended route between start and end points, as calculated by the Google Maps API

The Second request is with waypoints, so as you observe the results has more km or longer results, because you specify the waypoints that the API need to calculate.

For more information, I try to use both Directions API and Distance Matrix API.

https://maps.googleapis.com/maps/api/directions/json?origin=sydney,au&destination=perth,au

-

https://maps.googleapis.com/maps/api/distancematrix/json?origins=sydney+au&destinations=perth+au

As you observe, if you did not use waypoints in the Directions API. The Directions API results are just the same (tiny difference to the value, but the text in km is the same) with the Distance Matrix API.