Get expert guidance in a few days with a Laravel code review

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
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Tinkerwell logo

Tinkerwell

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

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
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
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
Chief: Run Claude Code on Large Projects with Task-Based Workflows image

Chief: Run Claude Code on Large Projects with Task-Based Workflows

Read article
Laravel OpenAPI CLI: Generate Artisan Commands from Your API Spec image

Laravel OpenAPI CLI: Generate Artisan Commands from Your API Spec

Read article
Improved Skill and Guideline Detection in Laravel Boost v2.2.0 image

Improved Skill and Guideline Detection in Laravel Boost v2.2.0

Read article
"The Vibes" — NativePHP Hosts a Day 3 after Laracon US image

"The Vibes" — NativePHP Hosts a Day 3 after Laracon US

Read article
What We Know About Laravel 13 image

What We Know About Laravel 13

Read article
New Colors Added in Tailwind CSS v4.2 image

New Colors Added in Tailwind CSS v4.2

Read article