Google map distancematrix API call


#1

1) Give a description of the problem
Google distance API failing

2) What is the expected behavior?
Return the value and set a variable for the driving distance with traffic

3) What is happening/not happening?
I get an error message in the log:

║║Error executing external web request: java.lang.reflect.UndeclaredThrowableException

4) Post a Green Snapshot of the pistonimage
(

5) Attach any logs (From ST IDE and by turning logging level to Full)

+1ms ╔Received event [Entryway Light].switch = on with a delay of 1041ms
+142ms ║RunTime Analysis CS > 16ms > PS > 42ms > PE > 84ms > CE
+145ms ║Runtime (44773 bytes) successfully initialized in 42ms (v0.3.104.20180323) (144ms)
+146ms ║╔Execution stage started
+153ms ║║Comparison (enum) on changes_to (string) on = true (0ms)
+155ms ║║Cancelling condition #30’s schedules…
+155ms ║║Condition #30 evaluated true (4ms)
+162ms ║║Comparison (string) 20:30 is_inside_of_range (string) 18:00 … (string) 22:00 = true (3ms)
+163ms ║║Condition #36 evaluated true (7ms)
+164ms ║║Cancelling condition #16’s schedules…
+165ms ║║Condition group #16 evaluated true (state changed) (15ms)
+167ms ║║Cancelling statement #37’s schedules…
+479ms ║║Executed virtual command setVariable (3ms)
+487ms ║║Executed virtual command setVariable (2ms)
+496ms ║║Calculating (string) :https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&departure_time=now&origins= + (string) 44 >> (string) :https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&departure_time=now&origins=44
+499ms ║║Calculating (string) :https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&departure_time=now&origins=44 + (string) , >> (string) :https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&departure_time=now&origins=44,
+502ms ║║Calculating (string) :https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&departure_time=now&origins=44, + (string) -93 >> (string) :https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&departure_time=now&origins=44,-93
+505ms ║║Calculating (string) :https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&departure_time=now&origins=44,-93 + (string) &destinations=45.010664,-93.46675700000003&key= >> (string) :https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&departure_time=now&origins=44,-93&destinations=45.010664,-93.46675700000003&key=
+507ms ║║Calculating (string) :https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&departure_time=now&origins=44,-93&destinations=45.010664,-93.46675700000003&key= + (string) REMOVED >> (string) :https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&departure_time=now&origins=44,-93&destinations=45.010664,-93.46675700000003&key=REMOVED
+509ms ║║Calculating (string) :https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&departure_time=now&origins=44,-93&destinations=45.010664,-93.46675700000003&key=REMOVED + (string) : >> (string) :https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&departure_time=now&origins=44,-93&destinations=45.010664,-93.46675700000003&key=REMOVED:
+514ms ║║Sending external web request to: maps.googleapis.com/maps/api/distancematrix/json?units=imperial&departure_time=now&origins=44,-93&destinations=45.010664,-93.46675700000003&key=REMOVED:
+515ms ║║Error executing external web request: java.lang.reflect.UndeclaredThrowableException
+517ms ║║Executed virtual command httpRequest (3ms)
+522ms ║║null
+526ms ║║Executed virtual command log (4ms)
+531ms ║║Executed virtual command setVariable (3ms)
+536ms ║║Calculating (string) It will currently take you + (string) Variable ‘$durationTime’ not found >> (string) It will currently take you Variable ‘$durationTime’ not found
+538ms ║║Calculating (string) It will currently take you Variable ‘$durationTime’ not found + (string) to get to work. >> (string) It will currently take you Variable ‘$durationTime’ not found to get to work.
+554ms ║║Executed virtual command sendNotificationToContacts (10ms)
+556ms ║╚Execution stage complete. (410ms)
+650ms ╚Event processed successfully (651ms)

I removed my API key.

So what I plan to do is when my GFs phone leaves the location, ‘House’, I retrieve the lat and long of her phone and pass it to the google map API. On the return, I was going to set a variable to the element, ‘duration_in_traffic.text’ and then pass it in a push notification to her phone.

For some reason I am getting an error on the return.


#2

I have something similar. I have 3 virtual switches that when turned on they will gather that info that you are looking for. Then they send that info to my sonos speaker.

Included is my piston. I’m sure you can pull out what=t you might need and adapt.


#3

I am really not following.

I think I could use the execute condition that once her phone presence changes away from, ‘Home,’ to get the current device lat and longitude then pass them back into the google API. My issue is the, ‘Error executing external web request: java.lang.reflect.UndeclaredThrowableException,’ message.

If I take the generated URL (filling in the lat/long from her device) and put it in a browser, it works just fine. It’s just calling it from webCoRE that trows that exception error.

I took someone else’s advice and change the commas between the lat/long to, ‘%2C,’ but even that did not fix the issue.


#4

Try using this address as an expression. For the get request

https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins={@HomeLat_Long}&destinations=‘ [Device_name:latitude] ‘%2C’ [Device_name:longitude]’&key=‘{@GoogleMapsDriveTimeApiKey}

I have my Home lat/long set as a global variable. And my api key as a global variable.

You could use either method having home lat/long set as global variable or you could populate it with [deviceName: latitude]’%2C’[Device:longitude]

Hope that may help


#5

I did get it to work as an expression and your example. Thanks!!

Does the GoPack come from the GB Packers? Born and raised in WI.


#6

Yup!


#7

So I have this working for the most part. I was wondering if there is any way to clean this up a little.

In the response message, I am taking the value, because I am later subtracting the non-traffic time from the traffic time to determine the delay. The value comes back in seconds and I had to create another variable (minutes) to do the conversion (seconds/60). Since I am pulling back 2 addresses (she may have to go to either site), it makes 6 variables. Is there a way I can do the math on the response and put that right into my ‘minutes’ variable?

Also, the way the distancematrix API works is that I can define one origin and multiple destinations. The response looks like this:

https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&departure_time=now&origins=44.7326303%2C-93.15094820000002&destinations=605%2028th%20Ave%20N.%20Plymouth,%20mn%2055447|6655%20Wedgwood%20Rd%20N,%20Maple%20Grove,%20MN%2055311&key=KEY

The basic format is: https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&departure_time=now&origins=ORIGN&destinations=DESTINATION 1|DESTINATION 2&key=KEY

I am having a formatting issue with, pipe, | character.

If I could figure that out, I could make a single call and just set my variable to the separate rows in the request.