Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions googleplaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def _fetch_remote_file(service_url, params=None, use_http_post=False):
return (response.headers.get('content-type'),
fn, response.read(), response.geturl())

locations_cache = {}

def geocode_location(location, sensor=False, api_key=None):
"""Converts a human-readable location to lat-lng.

Expand All @@ -111,17 +113,22 @@ def geocode_location(location, sensor=False, api_key=None):
raises:
GooglePlacesError -- if the geocoder fails to find a location.
"""
params = {'address': location, 'sensor': str(sensor).lower()}
if api_key is not None:
params['key'] = api_key
url, geo_response = _fetch_remote_json(
GooglePlaces.GEOCODE_API_URL, params)
_validate_response(url, geo_response)
if geo_response['status'] == GooglePlaces.RESPONSE_STATUS_ZERO_RESULTS:
error_detail = ('Lat/Lng for location \'%s\' can\'t be determined.' %
location)
raise GooglePlacesError(error_detail)
return geo_response['results'][0]['geometry']['location']
if location in locations_cache:
return locations_cache[location]
else:
params = {'address': location, 'sensor': str(sensor).lower()}
if api_key is not None:
params['key'] = api_key
url, geo_response = _fetch_remote_json(
GooglePlaces.GEOCODE_API_URL, params)
_validate_response(url, geo_response)
if geo_response['status'] == GooglePlaces.RESPONSE_STATUS_ZERO_RESULTS:
error_detail = ('Lat/Lng for location \'%s\' can\'t be determined.' %
location)
raise GooglePlacesError(error_detail)
response = geo_response['results'][0]['geometry']['location']
locations_cache.update({location: response})
return response

def _get_place_details(place_id, api_key, sensor=False,
language=lang.ENGLISH):
Expand Down