Hi All, I was actually using a method that is a little old. This has now been replaced. I've fleshed this out a little more to show only the duration but the working code is below. Replacing 'routes.duration' with '*' will return the full trip including turn by turn.
import os, sys, io
import M5
from M5 import *
import requests2
import network
import json
import requests
import math
rect0 = None
label1 = None
label0 = None
http_req = None
wlan = None
url = None
responsejson = None
timeseconds = None
sliced = None
def getETA():
global url, responsejson, timeseconds, label0, rect0, label1, http_req, wlan, sliced
payload = json.dumps({
"origin": {
"address": "32 London Bridge St, London SE1 9SG"
},
"destination": {
"address": "50 Lower Thames Street, London, EC3R 6DT"
},
"routingPreference": "TRAFFIC_AWARE"
})
headers = {
'X-Goog-FieldMask': 'routes.duration',
'Content-Type': 'application/json',
'X-Goog-Api-Key': '{{GMP API Key}}'
}
response = requests.request("POST", url, headers=headers, data=payload)
responsejson = json.loads((response.text))
timeseconds = responsejson["routes"][0]["duration"]
#sliced = slice(0,4)
#print(timeseconds[slice(4)])
print(str(math.trunc(int(timeseconds.strip("s"))/60)) + "m" )
label0.setText(str(math.trunc(int(timeseconds.strip("s"))/60)) + "m" )
def setup():
global label0, rect0, label1, http_req, wlan, url, timeseconds, responsejson, sliced
M5.begin()
Widgets.fillScreen(0xeeeeee)
label0 = Widgets.Label("Start", 317, 99, 1.0, 0x000000, 0xeeeeee, Widgets.FONTS.DejaVu72)
rect0 = Widgets.Rectangle(301, 52, 199, 199, 0x000000, 0xeeeeee)
label1 = Widgets.Label("Pitacs ETA", 336, 207, 1.0, 0x000000, 0xeeeeee, Widgets.FONTS.DejaVu24)
wlan = network.WLAN(network.STA_IF)
wlan.connect('<Network SSID>', '<Network Password>')
url = 'https://routes.googleapis.com/directions/v2:computeRoutes?key=<google maps api key>'
def loop():
global label0, rect0, label1, http_req, wlan, url, timeseconds, responsejson, sliced
M5.update()
if BtnA.isPressed():
getETA()
if name == 'main':
try:
setup()
while True:
loop()
except (Exception, KeyboardInterrupt) as e:
try:
from utility import print_error_msg
print_error_msg(e)
except ImportError:
print("please update to latest firmware")