clevyr/laravel-geocoder

Handles geocoding an address into lat/lng with multiple providers

v0.1.1 2022-08-22 21:27 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Handles geocoding an address into lat/lng based on cloud providers.

Note: Currently only supports using Google as a Geocoding provider. As we prioritize it, we will implement MapBox as well.

Installation

You can install the package via composer:

composer require clevyr/laravel-geocoder

Next, publish the plugin assets:

php artisan vendor:publish --provider="Clevyr\LaravelGeocoder\LaravelGeocoderServiceProvider"

This is the contents of the published config file (without descriptive comments):

return [
    'adapter' => 'google',
    'test-adapter' => 'test',
    'test' => [
        'lat' => 35.4771302,
        'lng' => -97.5283204,
    ],
];

Set up Google Geocoding

First, you need to set up a Google Geocoding API Key

Then, you will need to set the following ENV variable in your Laravel application:

GOOGLE_CLOUD_API_KEY=

Usage

use Clevyr\LaravelGeocoder\LaravelGeocoder;

$coords = LaravelGeocoder::GetLatLngFromAddress(
    '123 Test Ln',
    null,
    'New York',
    'NY',
    '10001',
);

echo $coords;

// [
//     "lat" => 40.7607184,
//     "lng" => -73.9613766,
// ]

Alternatively, you can include the Geocodable trait in your Laravel Eloquent model to get some nice geocoding instance methods:

use Clevyr\LaravelGeocoder\Traits\Geocodable;

class MyModel extends Model
{
    use Geocodable;
}

$model = MyModel::find(1);
$coords = $model->getLatAndLong();

$model->name = 'New Name';
$model->addressIsDirty(); // false

$model->address_line_1 = '123 Foo Ln.';
$model->addressIsDirty(); // true

Modifying the Model Geolocation Fields

By default, when you use the Geocodable trait, Laravel Geocoder will default to using the following fields on your model:

  • address_line_1 (Required)
  • address_line_2
  • city (Required)
  • state (Required)
  • postal_code (Required)
  • country (Defaults to 'US')

However, you can modify these fields on a per-model basis using the following getter functions in your model:

    // Override the postal_code property with something specific to this model
    public function getGeocoderPostalCodeAttribute()
    {
        return 'zip';
    }

This would assume that there is a zip field on this model, which will then be used for this model's geolocation. Here are the full list of getter functions that you can override:

public function getGeocoderAddressLine1Attribute();
public function getGeocoderAddressLine2Attribute();
public function getGeocoderCityAttribute();
public function getGeocoderStateAttribute();
public function getGeocoderPostalCodeAttribute();
public function getGeocoderCountryAttribute();

Testing

composer test

Linting

composer analyse

Laravel Pint

Laravel Pint is an opinionated PHP code style fixer for minimalists.

./vendor/bin/pint

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.