How to Geocode US & Canadian Addresses with Laravel

Published on by

How to Geocode US & Canadian Addresses with Laravel image

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:

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 L. Barnes photo

Eric is the creator of Laravel News and has been covering Laravel since 2012.

Filed in:
Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Forge
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
LoadForge logo

LoadForge

Easy, affordable load testing and stress tests for websites, APIs and databases.

LoadForge
Paragraph logo

Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Paragraph
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
Bacancy - Staff Augmentation logo

Bacancy - Staff Augmentation

Leave your web app development hustles to the leading IT Staff Augmentation Service Providers. Choose from an extensive pool of 1050+ developers and give yourself the sigh of success you deserve with Bacancy. Get In Touch Today!

Bacancy - Staff Augmentation
DocuWriter.ai logo

DocuWriter.ai

Save hours of manually writing Code Documentation, Comments & DocBlocks, Test suites and Refactoring.

DocuWriter.ai
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
October CMS v3.6 Ships Today, Full of New Features image

October CMS v3.6 Ships Today, Full of New Features

Read article
Laracon EU Videos are now out image

Laracon EU Videos are now out

Read article
Use Google's Gemini AI in Laravel image

Use Google's Gemini AI in Laravel

Read article
PhpStorm is getting a brand new terminal image

PhpStorm is getting a brand new terminal

Read article
Six Essential Plugins for Visual Studio Code image

Six Essential Plugins for Visual Studio Code

Read article
Modularize Your Laravel Application With the Modular Package image

Modularize Your Laravel Application With the Modular Package

Read article