PHP Geocoding Adapter

PHP Geocoding Adapter

Released 4 years ago , Last update 4 years ago

PHP adapter for external geocoding services

PHP Geocoding Adapter is a simple php component to integrate geocoding external services into your code. The component includes google geocoding adapter and open street map geocoding adapter. Adding a new service is as simple as extending Service class and adding business logic for the new service.

Requirements

  • PHP > 5.3
  • Allow allowurlfopen directive in your php.ini file

Adapters

  • Nominatim Adapter (Open Street Map)
  • Google Geocoding Adapter (Google Map)

Features

  • Search latitude and longitude from an address
  • Chain multiple services to have cascade fallback
  • Distance between two locations
  • Query cache interface

Pricing

14 day 14-day money-back guarantee

$29.99

Hosted License

  • Perpetual license

  • 1 site, unlimited servers

  • No distribution (hosted use only)

  • Commercial use

  • 6 months support

Need custom services for this product? Get a quote

Example

This is a complete example with multiple adapters:

require_once(__DIR__.'/../lib/Geo/Autoload.php');

$loader = new Geo\ClassLoader('Geo', dirname(__DIR__.'/../lib/Geo'));
$loader->register();


$search = new Geo\Search();
$search->addService(new Geo\Service\GoogleMap\GeoCode);

try
{
  $search->query('via Montenapoleone, Milano  ');
  $location_a = $search->getFirst();
  echo 'Address1: '.$location_a->getAddress().PHP_EOL;
  echo 'Latitude: '.$location_a->getLatitude().PHP_EOL;
  echo 'Longitude: '.$location_a->getLongitude().PHP_EOL.PHP_EOL;


}
catch(Exception $e)
{
  $services = $search->getServices();
  echo 'Status: '.$services[0]->getStatus().PHP_EOL;
  echo 'Service results: '.print_r($services[0]->getServiceResults()).PHP_EOL;
}


try
{
  $search->query('corso mazzini, Osimo');
  $location_b = $search->getFirst();
  echo 'Address2: '.$location_b->getAddress().PHP_EOL;
  echo 'Latitude: '.$location_b->getLatitude().PHP_EOL;
  echo 'Longitude: '.$location_b->getLongitude().PHP_EOL.PHP_EOL;
}
catch(Exception $e)
{
  $services = $search->getServices();
  echo 'Status: '.$services[0]->getStatus().PHP_EOL;
  echo 'Service results: '.print_r($services[0]->getServiceResults()).PHP_EOL;
}

if(isset($location_a) && isset($location_b)) 
{
  echo 'Distance from Address1 to Address2: '.$location_a->distance($location_b).' Km'.PHP_EOL;
}

Installation

To use PHP Geocoding Adapter in your project copy library wherever you want, then include Autoload file and register it in your configuration file or front controller. The ClassLoader construct take in input two arguments, the first one is the namespace and the second one is the path to library directory.

Example:

  • Working directory /var/www/myapp/
  • Library copied into /var/www/myapp/lib/vendor/GeoAdapter
  • Config file /var/www/myapp/config/config.php

In config file add the following code:

require_once(__DIR__.'/../lib/Geo/Autoload.php');

$loader = new Geo\ClassLoader('Geo', dirname(__DIR__.'/../lib/Geo'));
$loader->register();

How to use

The library implements two main functionalities:

  • search a location from an address
  • measure the distance between two location

A location is a PHP object with latitude, longitude and address parameters.

Search a location from an address

First of all you need to create an istance of Geo\Search class and then add one or more adapter services such as:

$search = new Geo\Search();
$search->addService(new Geo\Service\GoogleMap\GeoCode);

In this way we add the google map geocoding adapter.

To retrieve a location we need to call query method with a valid address and retrieve the first result with getFirst method:

$search->query('via Montenapoleone, Milano  ');
$location = $search->getFirst();

echo 'Address: '.$location->getAddress().PHP_EOL;
echo 'Latitude: '.$location->getLatitude().PHP_EOL;
echo 'Longitude: '.$location->getLongitude().PHP_EOL.PHP_EOL;

The output in console is:

Address1: Via Montenapoleone, 20121 Milan, Italy
Latitude: 45.4681572
Longitude: 9.1952884

Measure the distance between two location

If in you application you need to measure the distance between two location, you can do it calling the distance method of location object:

$search = new Geo\Search();
$search->addService(new Geo\Service\GoogleMap\GeoCode);

$search->query('Milano');
$location_a = $search->getFirst();

$search->query('Firenze');
$location_b = $search->getFirst();

$distance = $location_a->distance($location_b);

echo "Distance from Milano to Firenze is $distance Km".PHP_EOL;

Unit Test

All the code is covered by unit tests. It ensure the right behavior of all code. To run test execute the following command on console:

phpunit --bootstrap test/bootstrap.php test/

The output will be:

PHPUnit 3.7.21 by Sebastian Bergmann.

Configuration read from /Users/cphp/Sites/GeoAdapter/phpunit.xml.dist

...........

Time: 1 second, Memory: 3.75Mb

OK (11 tests, 28 assertions)
2 licenses, starting from From » $29.99 View Licenses

Get A Quote

What do you need?
  • Custom development
  • Integration
  • Customization / Reskinning
  • Consultation
When do you need it?
  • Soon
  • Next week
  • Next month
  • Anytime

Thanks for getting in touch!

Your quote details have been received and we'll get back to you soon.

or Get a quote

for customization or integration services


Or enter your name and Email
  • TJ Tom Jones 3 years ago
    Does this app allow you to add location using a csv file and then use only the locations that you add? Since it is a php code, can I modify it myself with using php code?