How to Geocode US & Canadian Addresses with Laravel
Published on by Eric L. Barnes
This is a sponsored post by Geocodio who is a US and Canadian geocoder that plays well with Laravel.
Have you ever tried making a map? What about figuring out what the timezone is for a particular address?
Then you’ve probably come across geocoding. Geocoding makes addresses computer-readable and turns them into coordinates. It goes the other direction, too, and describes turning coordinates into addresses to make them human-readable.
If you’ve worked with address data, you know it can often be messy. Addresses may need to be parsed, completed, and standardized so you can work with them. Or you might not really care about putting addresses on a map but instead need to know the household income for that area or figure out what the nearest major city is. When you have a lot of addresses, that can be a time-consuming process.
That’s why we’ve made it simple to get all of that for US and Canadian addresses with just a single API call. And since so much data, like Census data, is only available if you have the coordinates, we’ve made it easy to get all of that in one place.
And, since we’re built on Laravel, we’ve made sure to make it especially easy for other Laravel folks to use our geocoding API.
You can use Geocodio for real-time single address geocoding but also batch processing. Most geocoders prohibit batch geocoding and storing the data afterwards, but as developers ourselves, we found that frustrating. So we built our API to make batch geocoding simple and don’t put any restrictions on how you use the data. Whether you have a hundred or ten million addresses, you can run them all through Geocodio.
You can batch geocode up to 10,000 addresses or coordinates at the time, letting you process data in bulk without all the headaches. You can read more on this here: https://www.geocod.io/docs/?php#batch-geocoding
You can also upload and process spreadsheets with up to 5M records at a time if that’s what you need. Just head to https://www.geocod.io/upload/
Here’s how you can geocode US and Canadian locations with Laravel.
First off, you’ll need an API key. Creating an API key is free and you get up to 2,500 lookups for free per day, no credit card or calls with sales reps required. You can create an API key here.
Next, we’ll want to get set up with the Geocodio PHP library. You can install the package via composer:
$ composer require geocodio/geocodio-library-php
The package will be auto-discovered by newer Laravel versions, so the only thing left to do is to publish the config file:
$ php artisan vendor:publish --provider="Geocodio\GeocodioServiceProvider"
You can now go ahead and edit your config file at config/geocodio.php. You’ll need to add your API key to the config file or set the GEOCODIO_API_KEY
environment variable.
You will now be able to use the Geocodio
facade. (You can also use dependency injection, see our PHP library for details.)
Here’s what a basic lookup would look like:
$response = Geocodio::geocode('1109 N Highland St, Arlington, VA');
But let’s say we also wanted Census household income data and timezone.
$response = Geocodio::geocode('1109 N Highland St, Arlington, VA', ['acs-economics', 'timezone']);
The $response variable now contains a wealth of data. You can see the full example response in your browser here
The first thing you might be interested in is the matched latitude and longitude:
$firstResult = $response->results[0] ?? null; if ($firstResult) { $lat = $firstResult->location->lat; $lng = $firstResult->location->lng;}
You can also get the standardized address as well as accuracy information:
dd($firstResult);
So for example, if you wanted to extract just the median household income for that Census Block Group, here’s what you would do:
dd($firstResult->fields->acs->economics->{'Household income'});
The other fields you can request include:
-
cd
: Congressional districts and legislator data -
Stateleg
: State legislative districts -
School
: school districts (US) -
census
: US Census codes -
acs-demographics
: US Census Demographics -
acs-economics
: US Census Income Data -
acs-families
: US Census Household Data -
acs-housing
: US Census Housing Data -
acs-social
: US Census Education & Veteran Status Data -
riding
: Canadian federal electoral districts -
statcan
: Canadian statistical boundaries -
timezone
: Timezones
What is Geocodio?
Geocodio started as a small side project in 2014 and we now handle over two billion lookups per month. Today, tens of thousands of companies use Geocodio and it’s our full-time job. Our customers range from independent developers to companies whose products you probably use every day.
We’ve been built on Laravel since we launched in 2014 and are grateful to have had so much support from the Laravel community throughout the years. You may remember us from our Laracon 2017 talk, and one of our co-founders Mathias founded and ran the Capitol Laravel Meetup (Washington DC) for several years.
If you ever have any questions or suggestions, you can feel free to reach us, the founders, directly at hello@geocod.io.
Eric is the creator of Laravel News and has been covering Laravel since 2012.