Skip to content

assghard/php-geocoders

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

assghard php-geocoders - The geocoders library for PHP project

Simple PHP library which provides geocoding and reverse geocoding features using different providers such as Nominatim (OpenStreetMap), Google, etc. Get address by coordinates and get coordinates by address

Requirements

  • PHP >= 8.0
  • PHP cURL

Installation

Install latest release (for PHP 8+):

composer require assghard/php-geocoders

For older versions of PHP

If you use PHP 7+: composer require assghard/php-geocoders:dev-php7

If you use PHP 5.6: composer require assghard/php-geocoders:dev-php56

Usage

Nominatim geocoder

Add GeocodingService and NominatimGeocoderProvider in uses section

use Assghard\PhpGeocoders\GeocodingService;
use Assghard\PhpGeocoders\Providers\NominatimGeocoderProvider;
$geocoder = new NominatimGeocoderProvider();
$geocodingService = new GeocodingService($geocoder);

// Get coordinates by address
$geocodeData = $geocodingService->geocode($addressAsString);

// Get address by coordinates
$reverseData = $geocodingService->reverse($latitude, $longitude);

You can also provide country code/codes to Geocoder provider:

$geocoder = new NominatimGeocoderProvider('en');

Multiple country codes:

$geocoder = new NominatimGeocoderProvider(['en', 'pl']);

Google geocoder

use Assghard\PhpGeocoders\GeocodingService;
use Assghard\PhpGeocoders\Providers\GoogleGeocoderProvider;

...

$geocoder = new GoogleGeocoderProvider('GOOGLE_MAPS_API_KEY', 'en');
/**
 * "en" - English language code. See Google documentation: https://developers.google.com/admin-sdk/directory/v1/languages
 * 
 * Google Maps javascript API documentation: https://developers.google.com/maps/documentation/javascript/overview
 */
$geocodingService = new GeocodingService($geocoder);
$geocodeData = $geocodingService->geocode($addressAsString); // Get coordinates by address
$reverseData = $geocodingService->reverse($latitude, $longitude); // Get address by coordinates

License

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