Created
April 1, 2016 20:37
-
-
Save ignacioHermosilla/520352d2e5d5197e0e45c6757c2a5f9b to your computer and use it in GitHub Desktop.
obtain potencial pos client using google places
This file contains hidden or 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
| COMUNA_SEARCH = 'Vitacura' | |
| places = {} | |
| def build_url(info): | |
| lat = info['geometry']['location']['lat'] | |
| lng = info['geometry']['location']['lng'] | |
| return "http://maps.googleapis.com/maps/api/geocode/json?latlng={},{}&sensor=true&language=es".format(lat,lng) | |
| def get_administrative_area(url, area_level='administrative_area_level_3'): | |
| try: | |
| import json | |
| import urllib2 | |
| request = urllib2.urlopen(url) | |
| request_data = request.read() | |
| request_json = json.loads(request_data) | |
| if "results" in request_json: | |
| if 'address_components' in request_json['results'][0]: | |
| address = request_json['results'][0]['address_components'] | |
| for d in address: | |
| if 'types' in d: | |
| for t in d['types']: | |
| if area_level in t: | |
| return d["short_name"] | |
| except: | |
| pass | |
| return None | |
| def get_data(url): | |
| info = requests.get(url) | |
| data = info.json() | |
| next_page_token = data.get('next_page_token') | |
| medical_centers = [] | |
| for result in data['results']: | |
| url = build_url(result) | |
| comuna = get_administrative_area(url) | |
| if comuna == COMUNA_SEARCH: | |
| if not places.get(result['place_id']): | |
| if 'formatted_address' in result: | |
| places[result['place_id']] = {'name': result['name'], 'address': result['formatted_address']} | |
| print result['name'] | |
| return next_page_token | |
| import json | |
| import requests | |
| urls = [ | |
| 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?language=es&location=-33.4481853,-70.6605075&radius=50000&name=centro+medico&key=AIzaSyCGkVWkZ9A4SqNA3WRLLm-HJyEE06g8eDM', | |
| 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?language=es&location=-33.4481853,-70.6605075&radius=50000&name=clinica&key=AIzaSyCGkVWkZ9A4SqNA3WRLLm-HJyEE06g8eDM', | |
| 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?language=es&location=-33.4481853,-70.6605075&radius=50000&name=clinica&key=AIzaSyCGkVWkZ9A4SqNA3WRLLm-HJyEE06g8eDM', | |
| 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?language=es&location=-33.4481853,-70.6605075&radius=50000&name=stripcenter&key=AIzaSyCGkVWkZ9A4SqNA3WRLLm-HJyEE06g8eDM', | |
| 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=centros+medicos+Santiago&key=AIzaSyCGkVWkZ9A4SqNA3WRLLm-HJyEE06g8eDM', | |
| 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=stripcenter&key=AIzaSyCGkVWkZ9A4SqNA3WRLLm-HJyEE06g8eDM', | |
| 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=centros+medicos+Las+Condes&key=AIzaSyCGkVWkZ9A4SqNA3WRLLm-HJyEE06g8eDM'] | |
| for url in urls: | |
| next_page_token = get_data(url) | |
| while next_page_token: | |
| next_url = '{}&pagetoken={}'.format(url, next_page_token) | |
| next_page_token = get_data(next_url) | |
| for k,v in places.items(): | |
| print v['name'], '|', v['address'] | |
| url = 'https://maps.googleapis.com/maps/api/place/textsearch/json?query=centros+medicos+Santiago&location=-33.4481853,-70.6605075&key=AIzaSyCGkVWkZ9A4SqNA3WRLLm-HJyEE06g8eDM' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment