Traffic time from Google API


#21

seemingly no joy - do i have to use a specific piston? i’m just triggering this manually for now


#22

There is something wrong with the way the response is being retrieved and stored, as it doesn’t match what I am seeing from running the URL within a browser or using Postman:

[status:OK, destination_addresses:[Washington, DC, USA], origin_addresses:[Corona, Queens, NY, USA], rows:[[elements:[[duration:[text:4 hours 12 mins, value:15095], distance:[text:382 km, value:381912], status:OK]]]]]

You can see the JSON Parser isn’t able to process the data:

An online validator doesn’t like it either:


#23

I think the json data has already been parsed at that point. There’s no need to call parse json data, just use $response.rows[0]…


#24

Can’t believe I missed that… That worked!

image

image


#25

sorry guys had to get some sleep and do other stuff … glad you figured it out. :slight_smile:


#26

same - thanks for the overnight help! appreciate it!


#27

Damn, people are really pushing this webCoRE thing… what’s next? Moon landing APIs? :smiley: :smiley: :smiley: :smiley: :smiley:

Great job guys! :slight_smile:


#28

Don’t want to be a downer here, and I appreciate the work it took to get this operational, but using a GET request to the Google Distance Matrix API for trip duration does not include current traffic conditions (at least not with the free API key).

Am I mistaken, or is a premium (read: paid) key the only way to integrate the current traffic into your time to destination output?


#29

I just ran into the traffic question while setting up a “time to work” tile in Action Tiles. It turns out you have to ask the Google API for that info. You can use the same Distance Matrix API, but you have to also add &departure_time=now to your query. That will return the traffic information if you were to leave, well, now.

Also, getting just the time from the JSON ouput, you can use:
$response.rows[0].elements[0].duration_in_traffic.text

What did you end up doing/triggering with your piston? Would you mind sharing your code, minus the API Key, of course?


#30

I’m using a traffic check every 10 minutes to tell my wife how traffic is going to be on her way to work. If there’s less than a 10-minute delay, the light is green. If it’s more than 10, but not more than 20, it’s yellow. And if it’s more than a 20-minute delay, the light is red.

I’m sure there’s a much better way to do the IF evaluations that doesn’t just plow through them sequentially, but I’m brand new to Webcore and NOT AT ALL a programmer. I did put nearly everything into variables so you should be able to adapt this to your needs. :slight_smile:


#31

If you wanted to you could make use of a switch/case statement. Your switch would be your variable minutes in traffic. Then you can setup ranges of cases. So first case would be exactly like your if statement. So on and so forth.

Saying all that there isn’t anything wrong with how you are doing it though


#32

Right after I posted here, I found the Switch case thingy in the Wiki and gave it a go. I think it’s working, but right now I’ve tested it so many times that I’m sure I have about 10 of them looping. Searching now for a way to break the routine… lol


#33

For testing if you want to make sure your getting updated stuff you can add a push notification that’s says the color


#34

Do I have to change the address for the GET request? Right now it’s “:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx1:”


#35

Yes, you have to use the URL for the Google Maps API…
https://maps.googleapis.com/maps/api/distancematrix/json?units={units}&departure_time=now&origins={origin_address}&destinations={destination_address}&key={api_key}

Everything in {brackets} needs to be replaced by either hard coding or by a defined variable.


#36

Is there a preferred method of entering in an address? I ask because every time the piston runs I show a typical_seconds and minutes_in_traffic as 0?


#37

If I recall correctly if your address is

123 maple dr, town, ca, 90210

Then replace all spaces with +

123+maple+dr,+town,+ca,+90210

And you might not need a space or + after commas

123+maple+dr,town,ca,90210


#38

No.

Whatever I input, typical_seconds and minutes_in_traffic as 0 on @Equis piston.


#39

Paste your test without api key. Unless addresses give away personal info


#40