Skip to content

Instantly share code, notes, and snippets.

@jwatson
Created October 17, 2012 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwatson/3907677 to your computer and use it in GitHub Desktop.
Save jwatson/3907677 to your computer and use it in GitHub Desktop.
MapQuest Directions API Example
#!/usr/bin/env python
from __future__ import print_function
import json
import os
import requests
import sys
def main():
api_key = os.environ.get('MAPQUEST_API_KEY', None)
if api_key is None:
print("Environment variable `MAPQUEST_API_KEY` is missing!")
sys.exit(1)
params = {
'key': api_key,
'inFormat': "json",
'outFormat': "json",
}
data = {
'locations': [
{
'street': "1 Broadway, Cambridge, MA",
'type': "s",
'dragPoint': False,
},
{
'street': "24 Yawkey Way, Boston, MA 02215",
'type': "s",
'dragPoint': False,
},
],
'options': {
'shapeFormat': 'cmp',
'generalize': 0,
},
'mapState': {
'width': 320,
'height': 568,
'scale': 9000, # zoom-level 12
},
}
data = json.dumps(data, separators=(',',':'))
r = requests.post("http://www.mapquestapi.com/directions/v1/route",
params=params,
data=data)
print(json.dumps(r.json, indent=4))
if __name__ == "__main__":
main()
Copy link

ghost commented Nov 13, 2014

When using a recent (11/11/14) version of Requests - line 19 failed for me until I changed it to " 'key': urllib.unquote(api_key)," and imported urllib at line 10.

Also changed line 52 to " print(json.dumps(r.json(), indent=4))".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment