11 Dec 15:56
MSIOW LAB 8
import urllib.parse
import requests
import json
key = 'a'
def get_longitude_latitude(api_key, address):
url = f'https://api.openrouteservice.org/geocode/search?api_key={api_key}&text={address}'
response = requests.get(url)
data = response.json()
coordinates = data['features'][0]['geometry']['coordinates']
longitude, latitude = coordinates
return longitude, latitude
def get_route(api_key, start_longitude, start_latitude, end_longitude, end_latitude):
url = f'https://api.openrouteservice.org/v2/directions/driving-car?api_key={api_key}&start={start_longitude},{start_latitude}&end={end_longitude},{end_latitude}'
# print(url)
response = requests.get(url)
data = response.json()
# print(data)
# route = data['routes'][0]['segments'][0]['steps']
route = data['features'][0]['properties']['segments'][0]['steps']
dist = data['features'][0]['properties']['segments'][0]['distance']
duration = data['features'][0]['properties']['segments'][0]['duration']
return route, dist, duration
def main():
while True:
start = input("Podaj punkt początkowy: ")
end = input("Podaj punkt końcowy: ")
start_longitude, start_latitude = get_longitude_latitude(key, start)
end_longitude, end_latitude = get_longitude_latitude(key, end)
route, dist, duration = get_route(key, start_longitude, start_latitude, end_longitude, end_latitude)
print(f"\nTrasa z {start} do {end}:")
print(f"Dystans: {dist} m")
print(f"Przewidywany czas podrozy: {duration} sekund \n")
for step in route:
print(step['instruction'])
if __name__ == "__main__":
main()