lodge / postcode-lookup
A postcode lookup utility using the Google Maps API, ready to use with Laravel 5.
Installs: 24 938
Dependents: 0
Suggesters: 0
Security: 0
Stars: 32
Watchers: 2
Forks: 13
Open Issues: 4
Requires
- php: >=7.4
- illuminate/support: 4.x|~5.0|^6.0|^7.0|^8.0|^9.0
Requires (Dev)
- phpunit/phpunit: ^7.2|^8.4|^9.5
This package is auto-updated.
Last update: 2024-10-24 21:38:22 UTC
README
PHP library that performs a postcode lookup using Google Maps API.
Compatible with Laravel 4-8.
Quick start
First of all you need to require the package inside your composer.json
file:
{
"require": {
"lodge/postcode-lookup": "0.7.0"
}
}
Note: for Laravel 4 use version 0.3.0
Register with Laravel
Register the service provider. Inside your app.php
config file add:
'providers' => array(
...
'Lodge\Postcode\PostcodeServiceProvider',
)
A facade is also provided, so in order to register it add the following to your app.php
config file:
'aliases' => array(
...
'Postcode' => 'Lodge\Postcode\Facades\Postcode',
);
Publish the configuration file.
php artisan vendor:publish --provider="Lodge\Postcode\PostcodeServiceProvider"
Set your Google API key inside config/postcode.php
file.
Usage with Laravel
From within your controller you can call:
$postcode = Postcode::lookup('SW3 4SZ');
print_r($postcode);
// Outputs
array(
'postcode' => 'SW34SZ',
'street_number' => '',
'street' => '',
'sublocality' => '',
'town' => 'London',
'county' => 'Greater London',
'country' => 'United Kingdom',
'latitude' => 51.489117499999999,
'longitude' => -0.1579016
);
Get the Latitude and Longitude of an address
$coordinates = Postcode::getCoordinates($address);
print_r($coordinates);
// Outputs
array(
'latitude' => 1.521231
'longitude' => -23.012123
)
Usage outside of Laravel
// First of all you need to instantiate the class
// And assuming that you have required the composer
// autoload.php file
require 'vendor/autoload.php';
$googleApiKey = 'your-google-api-key';
$postcode = new Lodge\Postcode\Postcode($googleApiKey);
$results = $postcode->lookup('SW3 4SZ');
print_r($results);
// Outputs
array(
'postcode' => 'SW34SZ',
'street_number' => '',
'street' => '',
'sublocality' => '',
'town' => 'London',
'county' => 'Greater London',
'country' => 'United Kingdom',
'latitude' => 51.489117499999999,
'longitude' => -0.1579016
)
If you need to get just the latitude and longitude for an address you can use:
$googleApiKey = 'your-google-api-key';
$postcode = new Lodge\Postcode\Postcode($googleApiKey);
$results = $postcode->getCoordinates('SW3 4SZ');
print_r($results);
// Outputs
array(
'latitude' => 51.489117499999999
'longitude' => -0.1579016
)
Changelog
-
Version 0.7
- Added support for Laravel 8
-
Version 0.6
- Added support for Laravel 7
-
Version 0.4
- Added configuration file and the ability to set the Google API key.
- Updated namespaces to PSR-4
- Added test stubs.
- Dropped support for Laravel 4.
-
Version 0.3
- Removed deprecated
bind
call and instantiate as a singleton instead.
- Removed deprecated
-
Version 0.2
- Laravel 5 compatible
- Fixed facade namespace, that would not adhere to PSR-0
- Updated documentation
-
Version 0.1
- Initial Release
License
The MIT License (MIT)
Copyright (c) 2013-2015 Dan Ursu
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.