PHP Classes
elePHPant
Icontem

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    
Last Updated Ratings Unique User Downloads Download Rankings
2019-01-18 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 204 This week: 1All time: 8,143 This week: 429Up
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 nominee
January 2019
Nominee
Vote
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
  Performance   Level  
Name: Ray Paseur <contact>
Classes: 7 packages by
Country: United States United States
Innovation award
Innovation award
Nominee: 5x

Details
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] => 40°26'24.636"N
    [lng_dms] => 79°57'9.42"W
    [geo_dms] => 40°26'24.636"N,79°57'9.42"W
    [lat_dmd] => 40°26.4106'N
    [lng_dmd] => 79°57.157'W
    [geo_dmd] => 40°26.4106'N,79°57.157'W
    [lat_dd] => 40.4401766°N
    [lng_dd] => 79.9526167°W
    [geo_dd] => 40.4401766°N,79.9526167°W
    [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:

    require_once('class_Geocode.php'); 
    $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 40°26'24.636"N,79°57'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.

References:
    https://journeynorth.org/tm/LongitudeIntro.html
    https://en.wikipedia.org/wiki/Horizontal_position_representation
    https://en.wikipedia.org/wiki/Geographic_coordinate_system
    https://en.wikipedia.org/wiki/Decimal_degrees
    https://en.wikipedia.org/wiki/Open_Location_Code

  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  
 0%
Total:204
This week:1
All time:8,143
This week:429Up