PHP Geocoder

A very simple class that retrieves that lat/lng of a given location.

It uses the Google Maps API so head over to get an api key if you don’t already have one.

Example Usage

1
2
3
4
5
6
7
8
9
require( 'geocoder.php' );
$geocoder = new Geocoder(API_KEY);
$location = $geocoder->geocode($_GET['location']);
if ( $location->error ) {
    echo $location->error;
}
else {
    echo "Lat = " . $location->lat . ", Lng = " . $location->lng;
}

Returns an instance of stdClass with a lat and lng property

stdClass Object
(
    [lat] => -23.378941
    [lng] => 150.512323
)

Demo

This uses the Google Map API and the geocoder class to get a map of a given location.

This code uses file_get_contents which utilizes the fopen_wrappers that some hosts disable. It falls back on curl before finally giving up and returning an error.

Leave a Reply

You must be logged in to post a comment.