FORUMS
Remove All Ads from XDA

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

833 posts
Thanks Meter: 1,866
 
By MishaalRahman, Editor in Chief on 10th March 2016, 07:10 AM
Post Reply Email Thread
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:

    Quote:

    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:


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.
Attached Files
File Type: xml Best_Route.tsk.xml - [Click for QR Code] (2.4 KB, 2480 views)
The Following 18 Users Say Thank You to MishaalRahman For This Useful Post: [ View ] Gift MishaalRahman Ad-Free
 
 
10th March 2016, 01:41 PM |#2  
Member
Thanks Meter: 14
 
More
You are fast becoming my hero of this week. ๐Ÿ˜€
Working like a charm. Thanks๐Ÿ‘
The Following User Says Thank You to iamronin For This Useful Post: [ View ] Gift iamronin Ad-Free
10th March 2016, 07:08 PM |#3  
Junior Member
Thanks Meter: 1
 
More
Quote:
Originally Posted by MishaalRahman

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?
10th March 2016, 08:07 PM |#4  
MishaalRahman's Avatar
OP Editor in Chief
Thanks Meter: 1,866
 
More
Quote:
Originally Posted by krisjag


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.
10th March 2016, 08:11 PM |#5  
Junior Member
Thanks Meter: 1
 
More
Quote:
Originally Posted by MishaalRahman

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).
10th March 2016, 10:51 PM |#6  
Member
Thanks Meter: 7
 
More
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.
11th March 2016, 02:35 AM |#7  
MishaalRahman's Avatar
OP Editor in Chief
Thanks Meter: 1,866
 
More
Quote:
Originally Posted by tykob

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
11th March 2016, 04:04 AM |#8  
aquatarkus's Avatar
Member
Flag Toronto
Thanks Meter: 2
 
More
Some people also won't use Google Now because of how invasive it is towards personal privacy.

Sent from my LG-D801 using Tapatalk
11th March 2016, 02:11 PM |#9  
Junior Member
Thanks Meter: 1
 
More
Quote:
Originally Posted by tykob

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.
11th March 2016, 06:47 PM |#10  
Senior Member
Thanks Meter: 196
 
More
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"
14th March 2016, 10:57 AM |#11  
Senior Member
Thanks Meter: 219
 
More
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?
The Following User Says Thank You to TangerineTractor For This Useful Post: [ View ] Gift TangerineTractor Ad-Free
Post Reply Subscribe to Thread

Tags
commute, driving, google maps, google now, tasker

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes