Laravel: Get Latitude/Longitude from Address with Geocoder

https://laraveldaily.com/storage/330/16-18,-Argyle-Street,-London….png

Laravel Google Maps API Geocoder

While working on a demo project, I needed to get the geographical coordinates of a real estate object, by its address: street, postcode, city, and country. In this tutorial, I will show you how to use a package called GeocoderLaravel, to achieve this.

GeocoderLaravel package is a Laravel wrapper around another package called Geocoder PHP, created for easier use within Laravel framework specifically.

Both packages have a lot of features, but we’re interested in this scenario.

  • Example Input: “16-18, Argyle Street, Camden, London, WC1H 8EG, United Kingdom”
  • Example Output: [51.5291450, -0.1239401]

To do that, we need to use one of the 3rd party Geolocation services. There are a lot of them, including the ones that have API, but probably the most simple one is Google Maps API.

Important notice: using Google Maps API (and the majority of the other similar services) is not free. They may have some limited free usage, so check their pricing docs. Keep in mind that you need to watch the usage cause they often charge per API call.

Now, step by step.

Step 1. Install GeocoderLaravel Package

composer require toin0u/geocoder-laravel

php artisan vendor:publish --provider="Geocoder\Laravel\Providers\GeocoderService"

Simple, right?


Step 2. Add Google Maps API Key

Google Maps API Key looks something like this: AIzaSyAWRsRGOFbTXRlLHDOSudkerLjUtBfElUt. You can read here how to register and get that key.

After you do that, you can put it into the Laravel config.

GeocoderLaravel package comes with its config, supporting various providers of Geolocation.

config/geocoder.php:

return [

// ...

 

'providers' => [

Chain::class => [

GoogleMaps::class => [

env('GOOGLE_MAPS_LOCALE', 'us'),

env('GOOGLE_MAPS_API_KEY'), // <- THIS IS WHAT WE NEED

],

GeoPlugin::class => [],

],

],

 

// ...

];

As you can see, GoogleMaps is the provider enabled by default, so all you need to do is provide the API key in the .env file.

.env:

APP_NAME=Laravel

APP_ENV=local

APP_KEY=base64:wgvwuEojBNlCrmg7Pmn3x...

APP_DEBUG=true

 

## ...

 

GOOGLE_MAPS_API_KEY=AIzaSyAWRsRGOFbTXRlLHDOSudkerLjUtBfElUt

Notice: protect your .env file and this API key, cause, as I stated above, you are charged for API calls, so someone with that API key may directly damage you financially. Also, you can protect that API key from Google Maps API settings, restricting it by domain or other parameters.


Step 3. Call Geocoder For Coordinates

Here’s a snippet that will get you the data:

$address = "16-18, Argyle Street, Camden, London, WC1H 8EG, United Kingdom";

$result = app('geocoder')->geocode($address)->get();

$coordinates = $result[0]->getCoordinates();

$lat = $coordinates->getLatitude();

$long = $coordinates->getLongitude();

Since we’re using the global helper app('geocoder'), you can call it wherever you want: in Controller, Queued Job, Service class, etc.

Obviously, you are not guaranteed to get the results, especially if your address is inputted by your users. So, you should check if there are results returned.

Also, you may want to put this logic somewhere in the Job that would be fired and put into the queue, to avoid users waiting for a few seconds to get the API results.

So, simple piece of code, but a lot of caveats and potential things to go wrong. That said, when it does go well, it feels like simple magic!

Laravel News Links