File: demo_Geocode.php

Recommend this page to a friend!
  Classes of Ray Paseur  >  PHP Geocode Address Formatter  >  demo_Geocode.php  >  Download  
File: demo_Geocode.php
Role: Example script
Content type: text/plain
Description: Demonstration Script
Class: PHP Geocode Address Formatter
Parse a location string and returns its details
Author: By
Last change:
Date: 1 year ago
Size: 4,149 bytes
 

Contents

Class file image Download
<?php // classes/demo_Geocode.php
/**
 * Class Geocode Demonstration Script
 */
error_reporting(E_ALL);

// Load the Class
require_once('class_Geocode.php');

// Make sure that php works with UTF-8
mb_internal_encoding('utf-8');
mb_regex_encoding('utf-8');
mb_http_output('utf-8');


// Test Objects
Class TO
{
    public
$name, $geo;
    public function
__construct($name, $geo) {
       
$this->name = $name;
       
$this->geo = $geo;
    }
}


// A collection of curated examples, giving Geocode information in widely varying formats
$testdata=[];

// GOOD examples
$testdata[] = new TO( 'Post Office 15213 in Pittsburgh', '40.4401766,-79.9526167' );
$testdata[] = new TO( 'Post Office 15213 in Pittsburgh', '40.4401766N,79.9526167W' );
$testdata[] = new TO( 'Post Office 15213 in Pittsburgh', '4026.4106\'N,7957.157\'W' );
$testdata[] = new TO( 'Post Office 15213 in Pittsburgh', '4026\'24.636"N,7957\'9.42"W' );
$testdata[] = new TO( 'Post Office 15213 in Pittsburgh', '4026&apos;24.636&quot;N,7957&apos;9.42&quot;W' );
$testdata[] = new TO( 'Post Office 15213 in Pittsburgh', '4026&prime;24.636&Prime;N,7957&prime;9.42&Prime;W' );
$testdata[] = new TO( 'Post Office 15213 in Pittsburgh', '4026&rsquo;24.636&rdquo;N,7957&lsquo;9.42&ldquo;W' );
$testdata[] = new TO( 'Post Office 15213 in Pittsburgh', '4026&#8217;24.636&#8220;N,7957&#8217;9.42&#8220;W' );
$testdata[] = new TO( 'Post Office 15213 in Pittsburgh', '87G2C2RW+3X' );

$testdata[] = new TO( 'Seatac Airport (Google data)', '47 26.59&prime; -122 18.42&prime;' );
$testdata[] = new TO( 'Seatac Airport (Google data)', '47.443167, -122.307' );
$testdata[] = new TO( 'Seatac Airport (TFlite data)', 'N47 26.59 W122 18.42' );

$testdata[] = new TO( 'Sydney Opera House', '-33.8567844,151.2152966' );
$testdata[] = new TO( 'Sydney Opera House', '3351&prime;24.4&Prime;S 15112\'55.1"E' );

$testdata[] = new TO( 'The Taj Mahal', ' 27.1750151, 78.0421552' );
$testdata[] = new TO( 'The Taj Mahal', '27 10\'30.05 "North and 78 2\'31.76" East' );
$testdata[] = new TO( 'The Taj Mahal', '27.1750154 78.042155');
$testdata[] = new TO( 'The Taj Mahal', '7JVW52GR+2V');

// BAD examples
$testdata[] = new TO( 'Malformed LNG', 'Lat N48 42.29, Long W122.54.40' ); // Note two decimal points, a not-well-formed number
$testdata[] = new TO( 'Impossible LAT', '138.9304,-77.1481' );
$testdata[] = new TO( 'Impossible LNG', '38.9304,-277.1481' );
$testdata[] = new TO( 'Impossible LAT,LNG', '138.9304,-277.1481' );
$testdata[] = new TO( 'Coordinates not Geocode or OLC', '0' );
$testdata[] = new TO( 'Coordinates missing', '' );
$testdata[] = new TO( 'Coordinates goofy', 'gooseball' );


// Process each of the examples
echo '<meta charset="utf-8" />';
echo
'<pre>';
foreach (
$testdata as $to)
{
    echo
"<h3>$to->name: " . '<a target="_blank" href="https://www.google.com/maps?q=' . urlencode($to->geo) . '">' . $to->geo . '</a> </h3>';
   
$geothing = new Geocode($to->geo, $to->name);
    if (empty(
$geothing->error)) {
       
print_r($geothing);
        echo
'<a target="_blank" href="https://www.google.com/search?q=' . urlencode($geothing->geo) . '">' . $geothing->geo . '</a> computed geocode in D.ddd decimal' . PHP_EOL;
        echo
'<a target="_blank" href="https://www.google.com/search?q=' . urlencode($geothing->geo_dd) . '">' . $geothing->geo_dd . '</a> computed geocode in D.ddd decimal degrees' . PHP_EOL;
        echo
'<a target="_blank" href="https://www.google.com/search?q=' . urlencode($geothing->geo_dmd) . '">' . $geothing->geo_dmd . '</a> computed geocode in DM.mmm\' degrees decimal minutes' . PHP_EOL;
        echo
'<a target="_blank" href="https://www.google.com/search?q=' . urlencode($geothing->geo_dms) . '">' . $geothing->geo_dms . '</a> computed geocode in DM\'S.sss" degrees minutes decimal seconds' . PHP_EOL;
        echo
'<a target="_blank" href="https://plus.codes/' . urlencode($geothing->olc) . '">' . $geothing->olc . '</a> open location code link on plus.codes' . PHP_EOL;
    }
    else {
        echo
$geothing->error . PHP_EOL;
    }
    echo
PHP_EOL;
}


For more information send a message to info at phpclasses dot org.