I have added the dependency of Google Maps API Java Client in my Java project. I have a set of origin distances, destination distance and also teh GeoApiContext as such:
GeoApiContext context = new GeoApiContext().setApiKey(MY_API_KEY);
String[] destinationAddress = {"40.7127837,-74.0059413", "33.9533487,-117.3961564", "38.6270025,-90.19940419999999"};
String[] originAddress = {"12.8445,80.1523"};
I want to get the (Airplane?) Distances between these points. I know we can send a simple HTTP request like https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6655101,-73.89188969999998&destinations=40.690515%73.6334271&key=YOUR_API_KEY
But I want to use the JAVA Google Maps API. I have tried to do DistanceMatrixApiRequest s = DistanceMatrixApi.getDistanceMatrix(context, originAddress, destinationAddress);
but there is no getArrivalTimes
or getDistanceMatrix
or anyway to send request. I am very confused. Plz help. Thanks
The code snapshot is the following:
Well this is a complete answer of how to calculate distance and time with google distance matrix api between two places. If you are not using maven then you have to set these jars in your classpath
pom.xml
This is a class to send http request and get the data in json format
As I am using Spring ,so here is my Controller method to get the data
Now the Tough Part was iterating over JSON data to get the distance and time In the above method I was getting json data in variable response ,so here is the code to extract distance and time from it The response was something Like
Try this :
The DistanceMatrix response object is like that :