Lately I’ve been working on projects that require quick geocoding in the browser based on a user search. Here I want to share two specific geocoding services that have worked well for me, have been cost effective (free), and return seemingly solid results: Census and Mapbox. Both services allow for one to structure an HTTP query with an address and the response comes in JSON format with the appropriate latitude and longitude information.
The Census Geocoder
The Census Geocoder has a pretty simple REST API that is accessible without the need of a special API key or token, which is awesome. There are two valid ways to structure the query:
- One Line Address: allows you to send a single string into the query
- Address parameters: build the parameters for each part of the address
We’ll focus on the One Line Address for simplicity’s sake. The HTTP endpoint is as follows:
http://geocoding.geo.census.gov/geocoder/locations/onelineaddress?...
The necessary parameters are as follows:
address
– the address string you are looking forbenchmark
– A numerical ID or name that references what version of the locator should be searched. Here’s a list of benchmarks.format
– format of the response. Typically used withjson
orjsonp
An example call for the location of The Old Fashioned bar in Madison, WI with the address 23 North Pinkney St. Madison WI:
Mapbox Geocoder
Mapbox offers a great geocoding service that is very much like the Census, allowing for HTTP requests. They require an API key but beyond that you can make many calls to their API without going over the limit. One thing that draws me to the Mapbox version is the ability to make multiple queries for different addresses in the same request using their batch geocoding service (allowing up to 50 per request). Here’s an example searching for three different zip codes:
The response is an array of objects with the geometry information for the respective features.