pythonapiurlgoogle-distancematrix-apidistance-matrix

urllib.error.HTTPError: HTTP Error 404: Not Found using Google Matrix API


I am trying simply to get the distance matrix data but the url just seems not to be working. This is my code, I obviously didn't place the key but that's not the problem

import urllib.request
import json

key='x'

origin='2500+E+Kearney+Springfield+MO+65898'
destination='405+N+Jefferson+Ave+Springfield+MO+65806'

url=('https://maps.googleapis.com/maps/api/distancematrix/jason'
     +'?language=en-UK&units=metric'
     +'&origins={}'
     +'&destinations={}'
     +'&key={}'
     ).format(origin, destination, key)

response=urllib.request.urlopen(url)
response_json=json.loads(response.read())
distance_meters=response_json['rows'][0]['elements'][0]['distance']['value']
distance_minutes=response_json['rows'][0]['elements'][0]['duration']['value']/60

print("Origin: %s\nDestination: %s\nDistance (m): %s\nDuration (s): %s"
      %(origin,destination,distance_meters,round(distance_minutes,2)))

Solution

  • You have a typo in the url. You want to use https://maps.googleapis.com/maps/api/distancematrix/json instead of jason