Fork me on GitHub

Geocoder

The almost missing Geocoder PHP 5.3 library.

Easy to use • PHP 5.3+ • Lightweight • FreeGeoIp, HostIp, IpInfoDB, Yahoo! PlaceFinder, Google Maps, Bing Maps, OpenStreetMaps, Geoip, and CloudMade geocoding services • HTTP Adapters (Buzz, Curl, Guzzle, Zend) • 100% Agnostic • Unit tested

Street Addresses

"Eiffel Tower"
place name or address
48.8582, 2.2945
latitude, longitude

Geographic Coordinates

44.9817, -93.2783
latitude, longitude
350 7th St N, Minneapolis, MN
street address

IP Addresses

88.188.221.14
IP address
Orleans, France
street address

Installation

Get the code:

git clone git://github.com/willdurand/Geocoder.git


Or by using Composer by creating a composer.json file:

{
    "require": {
        "willdurand/geocoder": "*"
    }
}

Then, run these two commands to install it:

$ wget getcomposer.org/composer.phar
$ php composer.phar install

Now you can require the autoloader:

<?php

require 'vendor/.composer/autoload.php';


If you don't use neither Composer nor a ClassLoader in your application, just require the provided autoloader:

<?php

require_once 'path/to/geocoder/src/autoload.php';

You're done.






You need an HTTP Adapter to query an API. Then, you have to choose a provider which is closed to what you want to get. Geocoder provides a lot of providers, you can use one of them or write your own. You can also register all providers and decide later.

<?php

// Create an adapter
$adapter  = new \Geocoder\HttpAdapter\BuzzHttpAdapter();

// Create a Geocoder object and pass it your adapter
$geocoder = new \Geocoder\Geocoder();

// Then, register all providers your want
$geocoder->registerProviders(array(
    new \Geocoder\Provider\YahooProvider(
        $adapter, '<YAHOO_API_KEY>', $locale
    ),
    new \Geocoder\Provider\IpInfoDbProvider(
        $adapter, '<IPINFODB_API_KEY>'
    ),
    new \Geocoder\Provider\HostIpProvider($adapter),

    // your provider here
));

// Use it!
$result = $geocoder->geocode('Eiffel Tower');
// Or
$result = $geocoder->geocode('68.145.37.34');

API

Geocoding Reverse Geocoding
$geocoder->geocode('88.188.221.14');
IP address
$geocoder->reverse($latitude, $longitude);
geographic coordinates
$geocoder->geocode('10 rue Gambetta, Paris, France');
street address
<?php

$result = $geocoder->geocode('88.188.221.14');
// Result is:
// "latitude"       => string(9) "47.901428"
// "longitude"      => string(8) "1.904960"
// "bounds"         => array(4) {
//     "south" => string(9) "47.813320"
//     "west"  => string(8) "1.809770"
//     "north" => string(9) "47.960220"
//     "east"  => string(8) "1.993860"
// }
// "streetNumber"   => string(0) ""
// "streetName"     => string(0) ""
// "city"           => string(7) "Orleans"
// "zipcode"        => string(0) ""
// "county"         => string(6) "Loiret"
// "region"         => string(6) "Centre"
// "country"        => string(6) "France"

$result = $geocoder->geocode('10 rue Gambetta, Paris, France');
// Result is:
// "latitude"       => string(9) "48.863217"
// "longitude"      => string(8) "2.388821"
// "bounds"         => array(4) {
//     "south" => string(9) "48.863217"
//     "west"  => string(8) "2.388821"
//     "north" => string(9) "48.863217"
//     "east"  => string(8) "2.388821"
// }
// "streetNumber"   => string(2) "10"
// "streetName"     => string(15) "Avenue Gambetta"
// "city"           => string(5) "Paris"
// "county"         => string(5) "Paris"
// "zipcode"        => string(5) "75020"
// "region"         => string(14) "Ile-de-France"
// "country"        => string(6) "France"

$result = $geocoder->reverse($latitude, $longitude);


The $result object is an instance of the Geocoded class which implements ArrayAccess and the following API:

  • getCoordinates() will return an array with latitude and longitude values;
  • getLatitude() will return the latitude value;
  • getLongitude() will return the longitude value;
  • getBounds() will return an array with south, west, north and east values;
  • getStreetNumber() will return the street number/house number value;
  • getStreetName() will return the street name value;
  • getCity() will return the city value;
  • getZipcode() will return the zipcode value;
  • getCounty() will return the county value;
  • getRegion() will return the region value;
  • getCountry() will return te country value;
  • getCountryCode() will return the ISO country code.


The Geocoder's API is fluent, you can write:

<?php

$result = $geocoder
    ->registerProvider(new \My\Provider\Custom($adapter))
    ->using('custom')
    ->geocode('68.145.37.34')
    ;


The using() method allows you to choose the adapter to use. When you deal with multiple adapters, you may want to choose one of them. The default behavior is to use the first one but it can be annoying.

Providers

Geocoder comes with a lot of service providers:

IP-Based Address-Based
FreeGeoIp as IP-Based geocoding provider Google Maps as Address-Based geocoding and reverse geocoding provider
HostIp as IP-Based geocoding provider Bing Maps as Address-Based geocoding and reverse geocoding provider
IpInfoDB as IP-Based geocoding provider OpenStreetMaps as Address-Based geocoding and reverse geocoding provider
Yahoo! PlaceFinder as IP-Based geocoding provider CloudMade as Address-Based geocoding and reverse geocoding provider
Geoip PHP extension as IP-Based geocoding provider Yahoo! PlaceFinder as Address-Based geocoding and reverse geocoding provider

Dumpers

Geocoder provides dumpers that aim to transform a ResultInterface object in standard formats.

GPS eXchange Format GeoJSON
Keyhole Markup Language Well-Known Binary
Well-Known Text

Extending Things

You can provide your own adapter, you just need to create a new class which implements HttpAdapterInterface.

You can also write your own provider by implementing the ProviderInterface.

Note, the AbstractProvider class can help you by providing useful features.

You can provide your own dumper by implementing the DumperInterface.

About

Geocoder has been created by William Durand and awesome contributors.


The MIT License

Copyright (c) 2010-2011 william.durand1[at]gmail.com

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.

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.