Return to Snippet

Revision: 16817
at August 17, 2009 07:10 by seengee


Initial Code
<?
# this snippet can be used to see if $postcode is within $radius of $shop_long $shop_lat
$radius = trim($_GET['radius']);
$postcode = trim($_GET['postcode']);
$shop_long = 'LONGITUDE GOES HERE';
$shop_lat = 'LATITUDE GOES HERE';
$key = "GOOGLE_MAPS_API_KEY_GOES_HERE";
if(!strstr($postcode,' ')){
	$len = strlen($postcode);
	$stlen = ($len == 7) ? 4 : 3;
	$start = substr($postcode,0,$stlen);
	$end = substr($postcode,-3);
	$postcode = $start.' '.$end;
}
$postcode = $postcode." UK"; # hack for UK postcodes to work properly :(
$address = "http://maps.google.com/maps/geo?q=".urlencode($postcode)."&output=xml&key=$key";
$page = file_get_contents($address);
$xml = new SimpleXMLElement($page);
list($customer_longitude, $customer_latitude, $altitude) = explode(",",$xml->Response->Placemark->Point->coordinates);
?>
<h1>User Coordinates:</h1>
<ul>
	<li>Longitude:<?=$customer_longitude?></li>
	<li>Latitude :<?=$customer_latitude?></li>    
</ul>
<? 
$shop_longitude = (float) $shop_long;
$shop_latitude = (float) $shop_lat;
?>
<h1>Shop Coordinates:</h1>
<ul>
	<li>Longitude:<?=$shop_longitude?></li>
	<li>Latitude :<?=$shop_latitude?></li>    
</ul>
<? 

$radius = $radius; // in miles

$lng_min = $shop_longitude - $radius / abs(cos(deg2rad($shop_latitude)) * 69);
$lng_max = $shop_longitude + $radius / abs(cos(deg2rad($shop_latitude)) * 69);
$lat_min = $shop_latitude - ($radius / 69);
$lat_max = $shop_latitude + ($radius / 69);

$yn =((($customer_longitude >= $lng_min) && ($customer_longitude <=  $lng_max)) && (($customer_latitude >= $lat_min) && ($customer_latitude <=  $lat_max))) ? 'yes!':'nope!';

?><h1>Within <?=$radius?> miles ? <?=$yn?></h1>

Initial URL

                                

Initial Description

                                

Initial Title
Coordinate - Distance Lookup

Initial Tags

                                

Initial Language
PHP