Laravel Idea for PhpStorm - Full-featured IDE for productive artisans!

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.

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
Laravel Idea for PhpStorm logo

Laravel Idea for PhpStorm

Ultimate PhpStorm plugin for Laravel developers, delivering lightning-fast code completion, intelligent navigation, and powerful generation tools to supercharge productivity.

Laravel Idea for PhpStorm
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
Bacancy logo

Bacancy

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

Bacancy
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
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
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
Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate logo

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate

Build your SaaS application in hours. Out-of-the-box multi-tenancy and seamless Stripe integration. Supports subscriptions and one-time purchases, allowing you to focus on building and creating without repetitive setup tasks.

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate
JetShip - Laravel Starter Kit logo

JetShip - Laravel Starter Kit

A Laravel SaaS Boilerplate and a starter kit built on the TALL stack. It includes authentication, payments, admin panels, and more. Launch scalable apps fast with clean code, seamless deployment, and custom branding.

JetShip - Laravel Starter Kit
Rector logo

Rector

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

Rector
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
Handling Geospatial Data with Laravel Magellan image

Handling Geospatial Data with Laravel Magellan

Read article
Managing Large Datasets in Laravel with LazyCollection image

Managing Large Datasets in Laravel with LazyCollection

Read article
Collect and Monitor Everything About Sent Emails in Your Laravel App image

Collect and Monitor Everything About Sent Emails in Your Laravel App

Read article
Packagist.org is ending support for Composer 1.x image

Packagist.org is ending support for Composer 1.x

Read article
Mastering Dynamic String Manipulation with Laravel's Str::replaceArray() image

Mastering Dynamic String Manipulation with Laravel's Str::replaceArray()

Read article
Laravel Jobs - December image

Laravel Jobs - December

Read article