[GUIDE][Difficulty: Intermediate] Determine Best Route for Daily Commute

Search This thread

MishaalRahman

Editor in Chief
Nov 2, 2015
1,038
2,118
www.xda-developers.com
Using the Google Maps API, we can input a starting address and a destination address and it will spit out the best route to take as well as the estimated travel time. And yes, it accounts for current traffic and road conditions!

Credits to redditor Golden-Death for figuring this out!

Prerequisites

  • A Google Maps API account. I will quote the instructions you need to set up the account and the API from the thread above:

    First, get a google maps API account here: https://developers.google.com/maps/ Click "Get Started" and then click on Google Maps Android API. Then click on "Get a Key". Create a new project and name it whatever you want. Generate an API key, and take note of it. From here on out, I will refer to this key as YOUR_API_KEY.

    Click on the "Overview" button on the left tab (of the API manager screen), then expand the Google Maps APIs section. Click on "Google Maps Directions API", and then enable it at the top. From here, you can also visit the USAGE tab to see how many requests you have made for directions, or visit the QUOTAS tab to limit your daily quota to the free amount so you never get billed (or, at least that's my understanding).

Instructions

  1. Variables --> Variable Clear. Name %GMAPS.
  2. Variables --> Variable Set. Name %start, and set it to your starting address. Replace any spaces with a + sign. For example,
    Code:
    1600 Pennsylvania Ave NW, Washington, DC 20500
    becomes
    Code:
    1600+Pennsylvania+Ave+NW,+Washington,+DC+20500
  3. Variables --> Variable Set. Name %destination, and set it to your destination address. Use the same convention as above.
  4. Variables --> Variable Set. Name %api, and set it to your Google Maps API key.
  5. Net --> HTTP Get. Set Server:port to
    Code:
    https://maps.googleapis.com:443
    and set the Path to
    Code:
    /maps/api/directions/json?origin=%start&destination=%destination&travelmode=driving&departure_time=now&key=%api
    Remember to input your API key above!
  6. Variables --> Variable Set. Name %GMAPS to %HTTPD. This will set the output from the Google Maps API to a variable we will parse using a Javascriptlet.
  7. Code --> Javascriptlet. Here is the code you need:
    Code:
    var routeSplit = JSON.parse(global('GMAPS'));
    var routetraffic = routeSplit.routes[0].legs[0].duration_in_traffic.text;
    var routename = routeSplit.routes[0].summary;
  8. Alert --> Say. Text: Hello, you should take %routename to get to your destination the fastest. Expected travel time is %routetraffic.

How do you use this task, you ask? You can use it as an exit task when disconnecting from your home wifi. You can use it to trigger when connecting to your car's bluetooth. You can use a location trigger when leaving your home. You can use an NFC tag. The choice is yours.

Here's a screenshot of the task:
TJfa4VD.jpg


I've attached an XML of the task below. You can import it by moving it to /sdcard/Tasker/tasks, then opening Tasker and long-pressing on the tasks tab up top then pressing import.
 

Attachments

  • Best_Route.tsk.xml
    2.4 KB · Views: 2,600

krisjag

Member
Dec 30, 2010
5
1
Using the Google Maps API, we can input a starting address and a destination address and it will spit out the best route to take as well as the estimated travel time. And yes, it accounts for current traffic and road conditions!

Thanks for the contribution! This is so odd, I was just working on this yesterday evening. What I was trying to do, instead, was use custom routes from home to work (we all know that shortcut that brings us around the one traffic light or ramp that backs up) ... Google Maps indicates many of these shortcuts are in fact faster during traffic, but doesn't route using these because it involves counterintuitive maneuvering (for example, going north and then taking a u-turn to go south, instead of taking the south ramp which is sometimes backed up for miles). But I digress...

What I tried to do is create custom routes in Google Maps by dragging the route to where I wanted it to be, then using a HTTP GET to pull the html, a variable split to capture the travel time in minutes with traffic, and then printing it as a notification.

The goal being to have an on-demand decision maker before I hit the first option to split off onto a different route, then again before I hit the second option, and so on.

All I got was garbage returned, and truth be told it's been a decade since I wrote anything entirely in code.

Any suggestions?
 

MishaalRahman

Editor in Chief
Nov 2, 2015
1,038
2,118
www.xda-developers.com
Thanks for the contribution! This is so odd, I was just working on this yesterday evening. What I was trying to do, instead, was use custom routes from home to work (we all know that shortcut that brings us around the one traffic light or ramp that backs up) ... Google Maps indicates many of these shortcuts are in fact faster during traffic, but doesn't route using these because it involves counterintuitive maneuvering (for example, going north and then taking a u-turn to go south, instead of taking the south ramp which is sometimes backed up for miles). But I digress...

What I tried to do is create custom routes in Google Maps by dragging the route to where I wanted it to be, then using a HTTP GET to pull the html, a variable split to capture the travel time in minutes with traffic, and then printing it as a notification.

The goal being to have an on-demand decision maker before I hit the first option to split off onto a different route, then again before I hit the second option, and so on.

All I got was garbage returned, and truth be told it's been a decade since I wrote anything entirely in code.

Any suggestions?

One thing you could try is to run the multiple times and set the destination address to a different intermediate point on your route, so the best route will be determined from each marker point. For it to activate as soon as you reach that point on the map you would need a location context using geofences such as with AutoLocation.
 

krisjag

Member
Dec 30, 2010
5
1
One thing you could try is to run the multiple times and set the destination address to a different intermediate point on your route, so the best route will be determined from each marker point. For it to activate as soon as you reach that point on the map you would need a location context using geofences such as with AutoLocation.

Thanks... I read the source link a little deeper and it seems they're recommending something similar, doing individual routes and using Tasker to add the time (reported in seconds) and round into minutes. If anyone can think of a simpler way not involving an API, or that just uses HTTP GET to strip out the travel time using "via" points through Google Maps through a browser, I'm all ears! (apparently you can't do waypoints, that borks the traffic time report since it factors in a "stop" at each waypoint; you can't use via points, since you need to have at least one waypoint when using that).
 

tykob

Member
Mar 29, 2011
40
9
This seems cool but forgive me if I'm missing something here. How is this different from what Gmaps already does? When I am about to leave for work, I open google now and the card with my drivetime to work is already there on top. Tapping it opens maps where it usually (~95% of the time) is already set to what it perceives to be the fastest route based on current traffic conditions. Totally honest question, and I'm certainly not trying to downplay or discredit the work of the task's creator. I'm just failing to see what this is solving or improving upon.
 

MishaalRahman

Editor in Chief
Nov 2, 2015
1,038
2,118
www.xda-developers.com
This seems cool but forgive me if I'm missing something here. How is this different from what Gmaps already does? When I am about to leave for work, I open google now and the card with my drivetime to work is already there on top. Tapping it opens maps where it usually (~95% of the time) is already set to what it perceives to be the fastest route based on current traffic conditions. Totally honest question, and I'm certainly not trying to downplay or discredit the work of the task's creator. I'm just failing to see what this is solving or improving upon.

Many people don't use Google Now because of battery issues
 

krisjag

Member
Dec 30, 2010
5
1
This seems cool but forgive me if I'm missing something here. How is this different from what Gmaps already does? When I am about to leave for work, I open google now and the card with my drivetime to work is already there on top. Tapping it opens maps where it usually (~95% of the time) is already set to what it perceives to be the fastest route based on current traffic conditions. Totally honest question, and I'm certainly not trying to downplay or discredit the work of the task's creator. I'm just failing to see what this is solving or improving upon.

Google Now doesn't always give me the little ancillary improvements that I know from regional familiarity, it only gives me the "macro" routes. For instance, getting onto the state highway from the local access road, there is a ramp that backs up for about a half mile and goes really slow because of the merge. To circumvent it, I take the opposite direction on the highway and flip around at the next exit. It shaves 5 minutes off my commute but it's not something Google Maps or Google Now will recognize.

It also doesn't give me an option to refresh the commute time when I hit each geographic decision point, without just using Navigation outright.
 

papete

Senior Member
Jul 27, 2008
501
205
I just tried this, but I'm getting the following error (on GMAPS variable):

"This API project is not authorized to use this API. Please ensure that this API is activated in the API's Console" I doubled check my API key and seems right. Also, it looks active in the API's console. Any ideas what is going on??

Thanks

EDIT: I found out the problem: On the API manager console you must activate also the "Google Maps Directions API"
 
Last edited:

TangerineTractor

Senior Member
Sep 8, 2006
1,024
211
Hmmm. This is looking good, but I can't quite get it to work - I've put a popup at the end to display %routename, but it always displays actually "%routename" rather than the value. Does does the variable in the javascript need making public somehow?
 
  • Like
Reactions: astage

iamronin

Senior Member
Aug 1, 2015
92
17
Hmmm. This is looking good, but I can't quite get it to work - I've put a popup at the end to display %routename, but it always displays actually "%routename" rather than the value. Does does the variable in the javascript need making public somehow?

When you go to your Google console, Click on the "Overview" button on the left tab (of the API manager screen), then expand the Google Maps APIs section. Click on "Google Maps Directions API", and then enable it at the top. Console will generate a key when you enable it. Use this key, and the task will work.
 

TangerineTractor

Senior Member
Sep 8, 2006
1,024
211
When you go to your Google console, Click on the "Overview" button on the left tab (of the API manager screen), then expand the Google Maps APIs section. Click on "Google Maps Directions API", and then enable it at the top. Console will generate a key when you enable it. Use this key, and the task will work.

Got maps android and maps directions APIS both enabled. Still don't get anything in the variable.
 

xoad218

New member
Aug 14, 2015
3
4
Awesome. I've been playing around with this a lot and I got it to work exactly how I want it to for my 20~40 minute commute to work.

I have the task trigger whenever I connect to my car's bluetooth, with the if statements matching the certain time of the day, and day of the week (when I usually commute to work) so it doesn't trigger when I just go out to get groceries or run errands, etc.

I also modified it a little bit as well,
I added Clear %LOC, then Get Location and changed %start variable set to %LOC instead of a predefined starting address so that where ever I am I can get updated travel times, since I might have to stop by some place before I reach my destination.
No modifying elsewhere needed outside of that, all the actions you provided still take it as a proper starting point.

Thanks a lot for this. I've been looking for something like this for months. Really Appreciate it :good:
 

hgupta

Member
Oct 2, 2007
42
0
This works and is really nice. However, the travel time that it calculates (15 minutes) is lower than Google maps (19 minutes). Is anyone else getting the discrepancy? I just want make sure it accounts for traffic.
 

Meritz

Member
May 8, 2015
33
2
Great idea and thanks for posting a thorough guide! One tidbit for me is that I'm trying to get this to work on a public transport commute. I thought that maybe it could be as easy as changing the travel mode in the path of step 5 to transit but that didn't change the output of the alert. Any ideas?
 

Top Liked Posts

  • There are no posts matching your filters.
  • 18
    Using the Google Maps API, we can input a starting address and a destination address and it will spit out the best route to take as well as the estimated travel time. And yes, it accounts for current traffic and road conditions!

    Credits to redditor Golden-Death for figuring this out!

    Prerequisites

    • A Google Maps API account. I will quote the instructions you need to set up the account and the API from the thread above:

      First, get a google maps API account here: https://developers.google.com/maps/ Click "Get Started" and then click on Google Maps Android API. Then click on "Get a Key". Create a new project and name it whatever you want. Generate an API key, and take note of it. From here on out, I will refer to this key as YOUR_API_KEY.

      Click on the "Overview" button on the left tab (of the API manager screen), then expand the Google Maps APIs section. Click on "Google Maps Directions API", and then enable it at the top. From here, you can also visit the USAGE tab to see how many requests you have made for directions, or visit the QUOTAS tab to limit your daily quota to the free amount so you never get billed (or, at least that's my understanding).

    Instructions

    1. Variables --> Variable Clear. Name %GMAPS.
    2. Variables --> Variable Set. Name %start, and set it to your starting address. Replace any spaces with a + sign. For example,
      Code:
      1600 Pennsylvania Ave NW, Washington, DC 20500
      becomes
      Code:
      1600+Pennsylvania+Ave+NW,+Washington,+DC+20500
    3. Variables --> Variable Set. Name %destination, and set it to your destination address. Use the same convention as above.
    4. Variables --> Variable Set. Name %api, and set it to your Google Maps API key.
    5. Net --> HTTP Get. Set Server:port to
      Code:
      https://maps.googleapis.com:443
      and set the Path to
      Code:
      /maps/api/directions/json?origin=%start&destination=%destination&travelmode=driving&departure_time=now&key=%api
      Remember to input your API key above!
    6. Variables --> Variable Set. Name %GMAPS to %HTTPD. This will set the output from the Google Maps API to a variable we will parse using a Javascriptlet.
    7. Code --> Javascriptlet. Here is the code you need:
      Code:
      var routeSplit = JSON.parse(global('GMAPS'));
      var routetraffic = routeSplit.routes[0].legs[0].duration_in_traffic.text;
      var routename = routeSplit.routes[0].summary;
    8. Alert --> Say. Text: Hello, you should take %routename to get to your destination the fastest. Expected travel time is %routetraffic.

    How do you use this task, you ask? You can use it as an exit task when disconnecting from your home wifi. You can use it to trigger when connecting to your car's bluetooth. You can use a location trigger when leaving your home. You can use an NFC tag. The choice is yours.

    Here's a screenshot of the task:
    TJfa4VD.jpg


    I've attached an XML of the task below. You can import it by moving it to /sdcard/Tasker/tasks, then opening Tasker and long-pressing on the tasks tab up top then pressing import.
    4
    Awesome. I've been playing around with this a lot and I got it to work exactly how I want it to for my 20~40 minute commute to work.

    I have the task trigger whenever I connect to my car's bluetooth, with the if statements matching the certain time of the day, and day of the week (when I usually commute to work) so it doesn't trigger when I just go out to get groceries or run errands, etc.

    I also modified it a little bit as well,
    I added Clear %LOC, then Get Location and changed %start variable set to %LOC instead of a predefined starting address so that where ever I am I can get updated travel times, since I might have to stop by some place before I reach my destination.
    No modifying elsewhere needed outside of that, all the actions you provided still take it as a proper starting point.

    Thanks a lot for this. I've been looking for something like this for months. Really Appreciate it :good:
    2
    Thank you so much for sharing. I actually made a video with your steps and I added an auto notification
    You guys can check out my YouTube channel. Here is the direct link to the video,
    1
    You are fast becoming my hero of this week. ?
    Working like a charm. Thanks?
    1
    Hmmm. This is looking good, but I can't quite get it to work - I've put a popup at the end to display %routename, but it always displays actually "%routename" rather than the value. Does does the variable in the javascript need making public somehow?
Our Apps
Get our official app!
The best way to access XDA on your phone
Nav Gestures
Add swipe gestures to any Android
One Handed Mode
Eases uses one hand with your phone