PHP Classes

PHP Geocode Address Formatter: Parse a location string and returns its details

Recommend this page to a friend!
  Info   View files Example   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 242 All time: 7,926 This week: 204Up
Version License PHP version Categories
standard_geocode 1.0.0Public Domain7PHP 5, Text processing, Geography, Pa...
Description Author

This class can parse a location string and returns its details.

It takes a string with the address of a given location in many different formats that are supported and returns an object with many details of the location.

The object contains the latitude and longitude coordinates as decimal values or other formats like dms, dmd, dd and Open Location Plus Code.

Innovation Award
PHP Programming Innovation award winner
January 2019

Prize: One subscription to the PDF edition of the PHP Architect magazine
The location of one place in the world may be expressed in text using many different formats.

When you want to develop an application that allows your users to enter a location in all the selected formats, you need support whatever format the user may want to enter the location in text.

The class can parse a location string in many formats, thus providing flexibility for applications that need to processed locations entered manually by their users.

Manuel Lemos
Picture of Ray Paseur
  Performance   Level  
Name: Ray Paseur is available for providing paid consulting. Contact Ray Paseur .
Classes: 8 packages by
Country: United States United States
Age: 71
All time rank: 2311313 in United States United States
Week rank: 948 Up99 in United States United States Up
Innovation award
Innovation award
Nominee: 5x

Winner: 1x

Class Geocode

This is a lightweight class that normalizes geocodes (Lat,Lng pairs).  It has a single
method, the constructor, which receives a geocode string in any of several common and 
very relaxed formats.  See the "demo" file for examples.

The constructor attempts to interpret the input string.  It returns an instance of the
Geocode object with public properties as shown below.  If it's impossible to make sense
of the input, the "error" property will have some explanation of what went wrong.  If
"error" is not empty, you should not rely on the other values in the object.

This shows the Geocode object for the Post Office at ZIP code 15213 in Pittsburgh:

Geocode Object
    [name] => Post Office 15213 in Pittsburgh
    [lat] => 40.4401766
    [lng] => -79.9526167
    [geo] => 40.4401766,-79.9526167
    [lat_dms] => 4026'24.636"N
    [lng_dms] => 7957'9.42"W
    [geo_dms] => 4026'24.636"N,7957'9.42"W
    [lat_dmd] => 4026.4106'N
    [lng_dmd] => 7957.157'W
    [geo_dmd] => 4026.4106'N,7957.157'W
    [lat_dd] => 40.4401766N
    [lng_dd] => 79.9526167W
    [geo_dd] => 40.4401766N,79.9526167W
    [olc] => 87G2C2RW+3X
    [input] => 40.4401766,-79.9526167
    [error] => 

You must provide a geocode string or an open location code string to use the object.  
You may provide an optional name that can identify the place the geocode points to.

To get this object, create an instance of Geocode like these:

    $geo = new Geocode('40.4401766,-79.9526167', 'Post Office 15213 in Pittsburgh');

To use this object, first check $geo->error for empty() status, then you can get any
of the properties that make sense for your application, eg:

    echo $geo->geo_dms; // prints 4026'24.636"N,7957'9.42"W 
    echo $geo->olc;     // prints 87G2C2RW+3X 

Reconstruction of Lat,Lng pairs is accurate when using explicit geocodes because the
geocode points to a place on earth.  Accuracy is a bit less when using an open location 
code.  A 10-digit plus code has only about "rooftop" accuracy; the generated geocode 
will point to the southwest corner of the area defined by the OLC.


  Files folder image Files  
File Role Description
Plain text file class_Geocode.php Class The Geocode Class
Accessible without login Plain text file demo_Geocode.php Example Demonstration Script
Accessible without login Plain text file readme_Geocode.txt Doc. readme text file

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:7,926
This week:204Up
For more information send a message to info at phpclasses dot org.