We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

php geolocator

eejk
eejk asked
on
Medium Priority
404 Views
Last Modified: 2013-12-12
What is the sample code for a php geolocator.
Comment
Watch Question

Most Valuable Expert 2011
Author of the Year 2014

Commented:

<?php
require_once('_config.php');
 
// A FREEFORM LOCATION STATEMENT IS OPTIONAL (BUT NOT USED HERE)
$location = '';
if (!empty($_GET)) {
	foreach ($_GET as $k => $v) { $$k = get_clean_text_string($v); }
	if ($a != '') { $location .= $a . ' '; }
	if ($c != '') { $location .= $c . ' '; }
	if ($s != '') { $location .= $s . ' '; }
	if ($z != '') { $location .= $z . ' '; }
	$location = trim($location);
}
 
?>
<html><head><title><?=$a?> <?=$c?> <?=$s?> <?=$z?> Yahoo/Google Geocoder Query</title></head>
<body>
<form method="get">
Addr: <input type="text" name="a" autocomplete="off" value="<?=$a?>" />
City: <input type="text" name="c" autocomplete="off" value="<?=$c?>" />
ST:   <input type="text" name="s" autocomplete="off" value="<?=$s?>" size="2" />
Zip:  <input type="text" name="z" autocomplete="off" value="<?=$z?>" size="8" />
<input type="submit" name="_go" value="go" />
</form>
</body>
</html>
<?php
 
if (empty($_GET)) { die(); }
 
 
error_reporting(E_ALL);
 
 
// PREPARE THE GEOCODER
$y_demo			= new SimpleGeoCoder;
$y_demo->address	= $a;
$y_demo->city		= $c;
$y_demo->state		= $s;
$y_demo->zip		= $z;
 
// TEST THE YAHOO! GEOCODER
$y_demo->YahooGeoCode();
echo "<pre>\n\nYAHOO! ";
print_r($y_demo);
echo "</pre>\n";
 
 
// PREPARE THE GEOCODER
$g_demo			= new SimpleGeoCoder;
$g_demo->address	= $a;
$g_demo->city		= $c;
$g_demo->state		= $s;
$g_demo->zip		= $z;
 
 
// TEST THE GOOGLE GEOCODER
$g_demo->GoogleGeoCode();
echo "<pre>\n\nGOOGLE ";
print_r($g_demo);
echo "</pre>\n";
 
 
 
// LINK TO D/L SOURCE
reveal_source();
die();
 
 
 
// SIMPLE GEOCODER CLASS
class SimpleGeoCoder
{
 
// DECLARE THE INPUT DATA
public $location; // USE THIS FOR A FREEFORM QUERY, OR USE THESE PARTS
public $address;
public $city;
public $state;
public $zip;
 
// DECLARE THE WORKING DATA
private $precis; // TEXTUAL PRECISION FROM YAHOO
private $yfp, $yahoo_url,  $y_buf, $ydata;
private $gfp, $google_url, $g_buf, $gdata;
 
// DECLARE THE OUTPUT DATA
public $latitude;
public $longitude;
public $precision;
public $warning;
public $geocoder;
 
// DECLARE THE CONSTRUCTOR
public function __construct()
{
	$this->latitude		= 0.0;
	$this->longitude	= 0.0;
	$this->precision	= FALSE; // WANT A VALUE OF 5 OR HIGHER, HIGHER IS BETTER
	$this->warning		= '';
	$this->geocoder		= '';
}
 
 
// DECLARE THE YAHOO! VERSION OF THE WORKHORSE
public function YahooGeoCode()
{
	$this->geocoder	= 'Yahoo!';
	$yahoo_url	= "http://local.yahooapis.com/MapsService/V1/geocode?&appid=" . YAHOO_API;
 
	if ($this->location != '') // YOU CAN ASK FOR A FREEFORM QUERY
	{
		$yahoo_url	.= "&location=" . urlencode($this->location);
	}
	else // YOU CAN ASK FOR INDIVIDUAL PIECES OF AN ADDRESS
	{
		$yahoo_url	.= "&street=" . urlencode($this->address);
		$yahoo_url	.= "&city="   . urlencode($this->city);
		$yahoo_url	.= "&state="  . urlencode($this->state);
		$yahoo_url	.= "&zip="    . urlencode($this->zip);
	}
 
// EXECUTE YAHOO GEOCODER QUERY
// NOTE - USE ERROR SUPPRESSION OR IT WILL BARF UP THE YAHOO API - ON FAILURE RETURNS HTTP 400 BAD REQUEST
	if ($yfp = @fopen($yahoo_url, 'r'))
	{
		$y_buf	= '';
		while (!feof($yfp)) { $y_buf .= fgets($yfp); }
		fclose($yfp);
	}
	else
	{
		return FALSE;
	}
 
// EXAMINE THE RESULT
	if ($y_buf != '') // NOT EMPTY, WE GOT DATA
	{
		$ydata	= new SimpleXMLElement($y_buf);
 
// CHECK FOR ANY ERROR MESSAGE, IF NONE, EXTRACT THE DATA POINTS
		$y_err	= $ydata->Message;
		if ($y_err == '')
		{
			$this->precis		= $ydata->Result["precision"];
			$this->warning		= $ydata->Result["warning"];
			$this->latitude		= $ydata->Result->Latitude;
			$this->longitude	= $ydata->Result->Longitude;
			$this->address		= $ydata->Result->Address;
			$this->city		= $ydata->Result->City;
			$this->state		= $ydata->Result->State;
			$this->zip		= $ydata->Result->Zip;
 
// SET PRECISION TO A NUMBER VALUE
			if ($this->precis == 'zip')	{ $this->precision = "5"; }
			if ($this->precis == 'street')	{ $this->precision = "6"; }
			if ($this->precis == 'address')	{ $this->precision = "8"; }
		}
		else
		{
			return FALSE;
		}
	}
	else  // NO RESULT - SOMETHING IS SICK AT YAHOO
	{
		return FALSE;
	}
return TRUE;
} // END function YahooGeocode
 
 
// DECLARE THE GOOGLE VERSION OF THE WORKHORSE
public function GoogleGeoCode()
{
	$this->geocoder	= 'Google';
	$google_url	= "http://maps.google.com/maps/geo?key=" . GOOGLE_API . "&output=csv";
 
	if ($this->location != '') // YOU CAN ASK FOR A FREEFORM QUERY
	{
		$google_url	.= "&q=" . urlencode($this->location);
	}
	else // YOU CAN ASK FOR INDIVIDUAL PIECES OF AN ADDRESS
	{
		$google_url	.= "&q=" . urlencode(trim($this->address .' '. $this->city .' '. $this->state .' '. $this->zip));
	}
 
// EXECUTE GOOGLE GEOCODER QUERY
	if ($gfp = @fopen($google_url, 'r'))
	{
		$g_buf = '';
		while (!feof($gfp)) { $g_buf .= fgets($gfp); }
		fclose($gfp);
	}
	else
	{
		return FALSE;
	}
 
// EXTRACT THE DATA
	$gdata	= explode(',',$g_buf);
	if ($gdata[0] != '200') // RESPONSE CODE SHOULD BE '200' -- IF 602 - BAD ZIP CODE OR UNUSABLE ADDRESS
	{
		return FALSE;
	}
	$this->precision	= $gdata[1]; // GEOCODE ACCURACY - ZIP CODE = 5, HIGHER NUMBERS ARE BETTER
	$this->latitude		= $gdata[2];
	$this->longitude	= $gdata[3];
return TRUE;
} // END function GoogleGeocode
 
 
} // END class SimpleGeocoder

Open in new window

Most Valuable Expert 2011
Author of the Year 2014

Commented:
The config file contains the API keys.

Author

Commented:
where can i find The config file which contains the API keys.
_config.php
Most Valuable Expert 2011
Author of the Year 2014

Commented:
You can't have my API keys - they are used in my applications and would not be usable to you because they are registered for my URLs only.  You can get your own API keys free by registration from Google and Yahoo.

Author

Commented:
can you tell me the url to yahoo/google for geocoding
Most Valuable Expert 2011
Author of the Year 2014
Commented:
http://developer.yahoo.com/maps/rest/V1/geocode.html
http://code.google.com/apis/maps/documentation/services.html

There are other services available, such as Geogoder.US, but I found these two to be the most dependable and the fastest.

Good luck with your project, ~Ray

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Thanks for the points, and good luck with your project!  Best, ~Ray
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.