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
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.squareup.okio/okio -->
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>1.12.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.api-client/google-api-client -->
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.23.0</version>
</dependency>
This is a class to send http request and get the data in json format
package google.distance.api;
import java.io.IOException;
import org.springframework.stereotype.Component;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
@Component
public class DistanceTime {
private static final String API_KEY="YOUR KEY";
OkHttpClient client = new OkHttpClient();
public String calculate(String source ,String destination) throws IOException {
String url="https://maps.googleapis.com/maps/api/distancematrix/json?origins="+source+"&destinations="+destination+"&key="+ API_KEY;
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
}
As I am using Spring ,so here is my Controller method to get the data
private DistanceTime distance;
@Autowired
public void setDistance(DistanceTime distance) {
this.distance = distance;
}
public ModelAndView Api(@RequestParam("picking_up") String source,@RequestParam("dropping_off") String destination,@RequestParam("pick_up_date") String time) {
try {
//method of DistanceTime Class
String response=distance.calculate(source,destination);
System.out.println(response);
}
catch(Exception e) {
System.out.println("Exception Occurred");
}
return new ModelAndView("home");
}
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
{
"destination_addresses" : [
"Private"
],
"origin_addresses" : [ "Private" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "1,052 km",
"value" : 1051911
},
"duration" : {
"text" : "17 hours 10 mins",
"value" : 61785
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(response);
JSONObject jsonobj=(JSONObject)obj;
JSONArray dist=(JSONArray)jsonobj.get("rows");
JSONObject obj2 = (JSONObject)dist.get(0);
JSONArray disting=(JSONArray)obj2.get("elements");
JSONObject obj3 = (JSONObject)disting.get(0);
JSONObject obj4=(JSONObject)obj3.get("distance");
JSONObject obj5=(JSONObject)obj3.get("duration");
System.out.println(obj4.get("text"));
System.out.println(obj5.get("text"));
}
catch(Exception e) {
e.printStackTrace();
}