Outsource Laravel Development Partner - $3200/Month | Bacancy

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
Curotec

Curotec helps software teams hire the best Laravel developers in Latin America. Click for rates and to learn more about how it works.

Visit Curotec
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
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
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
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
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
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
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 →
Laravel 12.44 Adds HTTP Client afterResponse() Callbacks image

Laravel 12.44 Adds HTTP Client afterResponse() Callbacks

Read article
Handle Nested Data Structures in PHP with the Data Block Package image

Handle Nested Data Structures in PHP with the Data Block Package

Read article
Detect and Clean Up Unchanged Vendor Files with Laravel Vendor Cleanup image

Detect and Clean Up Unchanged Vendor Files with Laravel Vendor Cleanup

Read article
Seamless PropelAuth Integration in Laravel with Earhart image

Seamless PropelAuth Integration in Laravel with Earhart

Read article
Laravel API Route image

Laravel API Route

Read article
Laravel News 2025 Recap image

Laravel News 2025 Recap

Read article