PHP Classes

PHP PlusCode Local: Get the location information from PlusCode

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: 105 This week: 1All time: 9,693 This week: 571Up
Version License PHP version Categories
pluscodelocal 1.0.0Public Domain5PHP 5, Geography
Description 

Author

This class can get the location information from PlusCode.

It takes a string in the PlusCode format and extracts the geographic coordinates of the respective location using the base PlusCode.

The class uses the GeoNames API to get details about the given location and returns a PlusCode local string.

Innovation Award
PHP Programming Innovation award nominee
February 2019
Number 10
PlusCode is a form to represent a location in the planet.

This class can take a location string defined in the PlusCode format and returns a string with the administrative city, state and country.

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: 73
All time rank: 2240311 in United States United States
Week rank: 103 Up12 in United States United States Up
Innovation award
Innovation award
Nominee: 5x

Winner: 1x

Example

<?php // demo_PlusCodeLocal.php
/**
 * Documentation References:
 *
 * https://www.geonames.org/about.html
 * https://www.geonames.org/export/ws-overview.html
 * https://www.geonames.org/export/web-services.html#findNearby
 * https://www.geonames.org/export/codes.html
 */
error_reporting(E_ALL);

// DEPENDENCY!
// You must get your own api key from GeoNames.org
require_once('class_PlusCode.php');
require_once(
'class_PlusCodeLocal.php'); // <-- API Key goes in this script

echo '<pre>';


// A collection of curated examples
$latlng = array
(
'Google Earth default USA center in LAWRENCE, KANSAS' => ' 38.95939, -95.2654831'
, 'Washington Monument' => ' 38.8894838, -77.0352791'
, 'Post Office 15213 in Pittsburgh' => ' 40.4401766, -79.9526167'
, 'Ivy City Smokehouse in DC' => ' 38.9146348, -76.9855517'
, 'Seatac Airport, according to Google' => ' 47.4502535, -122.3110052'
, 'Eiffel Tower, Paris' => ' 48.8583701, 2.2944813'
, 'Sydney Opera House' => '-33.8567844, 151.2152966'
, 'Obelisco de Buenos Aires' => '-34.6037389, -58.3815703'
, 'Merlion, Marina Bay, Singapore' => ' 1.286833, 103.8545280'
, 'La Basilica del Voto Nacional, Quito, Ecuador' => ' -0.2144375, -78.5071875'
, 'Mu Pagoa Waterfall, Puleia Samoa' => '-13.7768121, -172.3786318'
, 'The Taj Mahal' => ' 27.1750151, 78.0421552'
)
;

// Process each of the examples
foreach ($latlng as $name => $spot) {
   
sleep(1);
   
$plus = PlusCode::geocode_to_olc($spot);
   
$local = PlusCodeLocal::get($plus);
   
$ulocal = urlencode($local);

    echo
PHP_EOL;
    echo
PHP_EOL . "<b>$name</b>";
    echo
PHP_EOL . 'Input Lat,Lng: <a target="_blank" href="https://www.google.com/maps?q=' . $spot . '">' . $spot . '</a>';
    echo
PHP_EOL . 'Computed PlusCode: <a target="_blank" href="https://plus.codes/' . $plus . '">' . $plus . '</a>';
    echo
PHP_EOL . 'Localized PlusCode: <a target="_blank" href="https://plus.codes/' . $ulocal . '">' . $local . '</a> via Plus.Codes/';
    echo
PHP_EOL . 'Localized PlusCode: <a target="_blank" href="https://www.google.com/maps?q=' . $ulocal . '">' . $local . '</a> via Google/Maps';
    echo
PHP_EOL;

}


Details

Class PlusCodeLocal This static class extends the static class PlusCode, found here: https://www.phpclasses.org/package/11016 The class provides a single method, get() which takes as input a full Open Location Code and returns a string with a localized code. The philosophy of the Google PlusCode address is that it "looks like a regular address, but with a short code where the street name and number would be." This class tries to provide the "regular address" part in a manner that looks similar to the PlusCode that Google might produce. It gives names of the nearby locations such that a full Open Location Code of "48Q39JW9+G9" would be returned in the form of a local code like "9JW9+G9 Buenos Aires Argentina." This class has a dependency on web services from GeoNames.org. To use the web services, you _must_ get your own API key from GeoNames. Register and login to GeoNames here: https://www.geonames.org/login When you have your API key, insert it into the public static $apikey variable at the top of this class file. The GeoNames API fails sometimes. When this happens the Class returns the original input Open Location Code. The full OLC may still be usable in many applications. A PHP Notice will be issued saying something like this: "ERROR: canceling statement due to statement timeout" The demonstration script for this class will call the GeoNames API repeatedly and in practice that might not be a good idea. You may want to consider downloading and hosting your own version of their database. Documentation References: https://plus.codes/ https://www.geonames.org/about.html https://www.geonames.org/export/ws-overview.html https://www.geonames.org/export/web-services.html#findNearby https://www.geonames.org/export/codes.html

  Files folder image Files  
File Role Description
Plain text file class_PlusCodeLocal.php Class PlusCode Localization
Accessible without login Plain text file demo_PlusCodeLocal.php Example Demonstration Script
Accessible without login Plain text file readme_PlusCodeLocal.txt Doc. readme text file

Downloadpluscodelocal-2019-01-13.zip 3KB
Downloadpluscodelocal-2019-01-13.tar.gz
Install with ComposerInstall with Composer
Needed packages  
Class DownloadWhy it is needed Dependency
PHP PlusCode Download .zip .tar.gz This Class extends PlusCode Required
 Version Control Unique User Downloads Download Rankings  
 0%
Total:105
This week:1
All time:9,693
This week:571Up