I am using distant matrix api to get the travel time between 2 points and I want to consider the traffics when calculating the travel time, I am calling the distance matrix while passing this URL as part of the request :
"https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&" +
"origins=" + startLat +"," + startlong +
"&destinations=" + endLat +","+endLong +
//departure_time parameter
"&departure_time=now" +
"&key=my api key");
According to the documentation adding the departure_time parameter to my request will add the travel time data while considering traffics (in the URL above I am using the parameter as the request is sent).
I want to pass some time in the future as the departure_time parameter
After reading the documentation it seems like I need to pass the time as int/long, (for example departure_time=1399995076).
Here is some time for example = 21:15 lets say on friday.
How can I convert this time or any time into int/long?
I have looked at this answer and many more related to the subject but none of them helped me.
Departure time need the time in milliseconds, to get the current time in milliseconds you need to use System.currentTimeMillis()
.
If you need time in the future add the numbers of hours in milliseconds, for example for 1 hour into the future is 3600000 milliseconds so use System.currentTimeMillis() + 3600000
.
So in your code it will look like this:
Long future_time = System.currentTimeMillis() + 3600000;
"https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&" +
"origins=" + startLat +"," + startlong +
"&destinations=" + endLat +","+endLong +
//departure_time parameter
"&departure_time=" + future_time +
"&key=my api key");