Maingraphhopper_parse-json_2.py
import requests
import urllib.parse
import json
route_url = "https://graphhopper.com/api/1/route?"
loc1 = "Toulouse, France"
loc2 = "Marseille, France"
key = "78f7571a-bb13-4357-ad8d-f4209d1d4ea9"
geocode_url = "https://graphhopper.com/api/1/geocode?"
def geocoding(location, key):
url = geocode_url + urllib.parse.urlencode({"q": location, "limit": "1", "key": key})
replydata = requests.get(url)
reply_status = replydata.status_code
print("Geocoding API URL for " + location + ":\n" + url)
if reply_status == 200:
json_data = replydata.json()
lat = json_data["hits"][0]["point"]["lat"]
lng = json_data["hits"][0]["point"]["lng"]
else:
lat = "null"
lng = "null"
return reply_status, lat, lng
orig = geocoding(loc1, key)
print(orig)
dest = geocoding(loc2, key)
print(dest)