Created
October 17, 2012 19:43
-
-
Save jwatson/3907677 to your computer and use it in GitHub Desktop.
MapQuest Directions API Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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))".