Skip to content

Instantly share code, notes, and snippets.

@dannguyen
Last active November 24, 2015 13:24
Show Gist options
  • Save dannguyen/f6d14be4d917961b50b2 to your computer and use it in GitHub Desktop.
Save dannguyen/f6d14be4d917961b50b2 to your computer and use it in GitHub Desktop.
Example of using Mapzen's Search API to do geocoding, using Python 3.x and the requests library. More advanced version here: https://gist.github.com/dannguyen/036a62e32fc9f6765b82
# Example of using Mapzen Search for free geocoding
# https://mapzen.com/documentation/search/search/
# result of geocoding all of New York's Starbucks: http://bit.ly/1P34T7h
from os import environ
import requests
API_KEY = environ['MAPZEN_SEARCH_KEY']
BASE_URL = 'https://search.mapzen.com/v1/search'
txt = '1007 MORRIS PARK AVE, 10462'
resp = requests.get(BASE_URL, params = {'api_key': API_KEY, 'size': 1, 'text': txt})
data = resp.json()
bbox = data['bbox']
# The data object returns a `bbox` attribute; by definition, for a single point
# the two points will be the same, e.g.
# 'bbox': [-73.85607, 40.848467, -73.85607, 40.848467]
# However, we take the average just in case the sole result is some kind of shape
pt = {'longitude': (bbox[0] + bbox[2]) / 2, 'latitude': (bbox[1] + bbox[3]) / 2 }
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment