Created
October 19, 2014 01:08
-
-
Save mkowoods/9a0aa69e5ea034a8ff2e to your computer and use it in GitHub Desktop.
Class to Access Google Maps API
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
import urllib | |
import json | |
""" | |
https://developers.google.com/maps/documentation/geocoding/ | |
https://developers.google.com/maps/documentation/distancematrix/ | |
""" | |
class GoogleMapsAPI(object): | |
def __init__(self): | |
self.domain = "https://maps.googleapis.com" | |
@staticmethod | |
def retrieve_url(url): | |
response = json.loads(urllib.urlopen(url).read()) | |
return response | |
def get_geocode_query_url(self, input_address): | |
path = u'/maps/api/geocode/json' | |
query = [(u'address',input_address),(u'sensor',u'false')] | |
url = u'%s?%s' % (self.domain+path, urllib.urlencode(query)) | |
return url | |
def get_distance_matrix_url(self, origin, destination): | |
origin | |
path = u'/maps/api/distancematrix/json' | |
query = [(u'origins', origin), (u'destinations', destination), (u'sensor', u'false')] | |
url = u'%s?%s' % (self.domain+path, urllib.urlencode(query)) | |
return url | |
G = GoogleMapsAPI() | |
url = G.get_geocode_query_url('paris, france') | |
print G.retrieve_url(url) | |
url2 = G.get_distance_matrix_url('6500 Hollister Ave, Goleta, CA', '521 N. Soledad #A, Santa Barbara, CA') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment