Geocoding in the browser

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:

  1. One Line Address: allows you to send a single string into the query
  2. 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:

The necessary parameters are as follows:

  • address – the address string you are looking for
  • benchmark – 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 with json or jsonp

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:;20009;22209.json?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6IlhHVkZmaW8ifQ.hAMX5hSW-QnTeRCMAy9A8Q

The response is an array of objects with the geometry information for the respective features.