From two addresses, get distance and route summary - using google api.
Posted: Sat Aug 11, 2018 12:19 am
Hi
This script uses google api,
it gets two addresses as input and retrieves the distance in Km and a summary of the route.
It is started by the start() function.
The script usually works. However, for bigger distances, the large reply by google api causes a memory error and the exception gets called.
A simpler way using urequests is what I have tried first, but it gets even more memory errors.
Do you know how to improve the code and make it work? If so, please show it.
thanks
This script uses google api,
it gets two addresses as input and retrieves the distance in Km and a summary of the route.
It is started by the start() function.
Code: Select all
import socket
def start():
found = 0
origem = input("origem:")
origem = origem.replace(" ",",")
destino = input("destino:")
destino = destino.replace(" ",",")
a = socket.getaddrinfo("googleapis.com", 80)
a = a[0][-1]
s = socket.socket()
s.connect(a)
s.sendall("GET /maps/api/directions/json?origin=")
s.sendall(origem)
s.sendall("&destination=")
s.sendall(destino)
s.sendall("&sensor=false")
s.sendall("\r\n\r\n")
while True:
try:
linha = str(s.readline(), 'utf8')
if "km" in linha:
if found == 1: continue
print(linha.split('"')[3])
found = 1
elif "ummary" in linha:
print(linha.split('"')[3])
s.close()
break
except:
print("Exception.\r\n")
s.close()
break
A simpler way using urequests is what I have tried first, but it gets even more memory errors.
Do you know how to improve the code and make it work? If so, please show it.
thanks