<?php
class nearbyPlugin {
function nearbyList()
{
$var_location = $_REQUEST['location'];
$var_city = $_REQUEST['city'];
$var_region = $_REQUEST['region'];
$var_country = $_REQUEST['country'];
$nearby_radius = $_REQUEST['r_radius'];
$geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$var_location.'+'.$var_city.'+'.$var_region.'+'.$var_country.'&sensor=false');
$output = json_decode($geocode);
$latitude = $output->results[0]->geometry->location->lat;
$longitude = $output->results[0]->geometry->location->lng;
//echo $latitude.":".$longitude;
$var_places = $this->setNearbyArray($latitude,$longitude,$nearby_radius);
if ( isset($var_places[0]['geoplugin_place']) )
{
echo "<pre><p>Some places you may wish to visit near " . $_REQUEST['location'] . ": </p>\n";
foreach ( $var_places as $key => $array )
{
$way_distance = $this->distance($latitude, $longitude, $array['geoplugin_latitude'], $array['geoplugin_longitude'], "k") ;
echo ($key + 1) .":<br />";
echo "\t Place: " . $array['geoplugin_place'] . "<br />";
echo "\t State: " . $array['geoplugin_region'] . "<br />";
echo "\t Latitude: " . $array['geoplugin_latitude'] . "<br />";
echo "\t Longitude: " . $array['geoplugin_longitude'] . "<br />";
echo "\t Distance: " . $way_distance . " Kilometer<br />";
}
echo "</pre>\n";
}
}
function setNearbyArray($latitude,$longitude,$nearby_radius)
{
//No of maximum places to be returned
$nearby_listcount = 20;
$host = 'http://www.geoplugin.net/extras/nearby.gp?lat='.$latitude.'&long='.$longitude.'&limit='.$nearby_listcount.'&radius='.$nearby_radius.'';
$nearby = array();
$response = $this->fetch($host);
$nearby = unserialize($response);
return $nearby;
}
function fetch($host)
{
if ( function_exists('curl_init') )
{
//use cURL to fetch data
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'geoPlugin PHP Class v1.0');
$response = curl_exec($ch);
curl_close ($ch);
}
else if ( ini_get('allow_url_fopen') )
{
//fall back to fopen()
$response = file_get_contents($host, 'r');
}
else
{
trigger_error ('geoPlugin class Error: Cannot retrieve data. Either compile PHP with cURL support or enable allow_url_fopen in php.ini ', E_USER_ERROR);
return;
}
return $response;
}
function distance($lat1, $lon1, $lat2, $lon2, $unit)
{
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.609344);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
}