2017 © Pedro Peláez
 

library laravel-location

A location reverser for Laravel with Google API

image

noprotocol/laravel-location

A location reverser for Laravel with Google API

  • Wednesday, March 29, 2017
  • by Thorazine
  • Repository
  • 10 Watchers
  • 2 Stars
  • 89 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 2 Open issues
  • 5 Versions
  • 16 % Grown

The README.md

Laravel Location

Get a complete location set from coordinates, address, postal code or IP. Through the Location Facade you can request the Google and IpInfo API to return the address of a visitor on your website. This script works out of the box, no need for any keys or registrations., (*1)

What you should keep in mind

This script uses the Google an IpInfo API to request information. Especially with the Ip API there is margin for error. The Google API is quite accurate and does most of the heavy lifting. However, please don't use this data as fact but rather as indication., (*2)

PHP requirements:

  • PHP Curl

To do:

  • Create Guzzle client support switch

Why not yet:

Guzzle continously changes it's workings. I haven't found the time yet., (*3)

How to make it work

Run:, (*4)

composer require noprotocol/laravel-location

Add to app/config => providers, (*5)

Noprotocol\LaravelLocation\LocationServiceProvider::class,

Add to app/config => aliases, (*6)

'Location' => Noprotocol\LaravelLocation\Facades\LocationFacade::class,

Get the configuration:, (*7)

php artisan vendor:publish --tag=location

If you have a Google key add a line to your .env file:, (*8)

GOOGLE_KEY=[key]

Script will work out of the box without a key, but it has limited requests. Please look at Google documentation hell to see how what the rate limiting is., (*9)

These (quick examples):, (*10)

$location = Location::locale('nl')->coordinatesToAddress(['latitude' => 52.385288, 'longitude' => 4.885361])->get();

$location = Location::locale('nl')->addressToCoordinates(['country' => 'Nederland', 'street' => 'Nieuwe Teertuinen', 'street_number' => 25])->get();

$location = Location::locale('nl')->postalcodeToCoordinates(['postal_code' => '1013 LV', 'street_number' => '25'])->coordinatesToAddress()->get();

$location = Location::locale('nl')->ipToCoordinates()->coordinatesToAddress()->get(); // if IP resolves properly, which it mostly doesn't

Will all result in:, (*11)

$location['latitude'] = 52.385288,
$location['longitude'] = 4.885361;
$location['country'] = 'Nederland';
$location['region'] = 'Noord-Holland';
$location['city'] = 'Amsterdam';
$location['street'] = 'Nieuwe Teertuinen';
$location['street_number'] = '25';
$location['postal_code'] = '1013 LV';

To return it as object set the get() function to true: get(true), (*12)

Extended example:

try {
    $location = Location::coordinatesToAddress(['latitude' => 52.385288, 'longitude' => 4.885361])->get(true);

    if($error = Location::error()) {
        dd($error);
    }
}
catch(Exception $e) {
    dd($e->getMessage());
}

The result is the default template and starts out as empty and gets filled throught the call. So if no data is available the result for that entry will be "". After every call the script resets to it's initial settings., (*13)

Chainable functions and their variables

Functions Values Validation Type
coordinatesToAddress() latitude required float
longitude required float
addressToCoordinates() country recommended string
region string
city recommended string
street required string
street_number required string
postalcodeToCoordinates() postal_code required string
street_number recommended string
get() true/false boolean boolean

Other functions

Functions Values Result
error() none Returns any error if there is one
response() none Returns the raw response from the Google API

Debug

With the try catch you can alreay see what you need. But besides this there is also a cached result of the raw response from the google API. Please note that this is not the case with the ip request., (*14)

$location = Location::coordinatesToAddress(['latitude' => 52.385288, 'longitude' => 4.885361])->get();
Location::response(); // results in raw api response

The Versions

29/03 2017

dev-master

9999999-dev

A location reverser for Laravel with Google API

  Sources   Download

MIT

by Matthijs Openneer

29/03 2017

1.0.0

1.0.0.0

A location reverser for Laravel with Google API

  Sources   Download

MIT

by Matthijs Openneer

07/03 2017

0.0.3

0.0.3.0

A location reverser for Laravel with Google API

  Sources   Download

MIT

by Matthijs Openneer

06/03 2017

0.0.1

0.0.1.0

A location reverser for Laravel with Google API

  Sources   Download

MIT

by Matthijs Openneer

06/03 2017

0.0.2

0.0.2.0

A location reverser for Laravel with Google API

  Sources   Download

MIT

by Matthijs Openneer