Find location or near by location in PHP
Hey guys I was wondering if there is a script that allows me to find a pretty accurate or near by location of a user using PHP.
I needed this to be implement in my code so wanted to see if anyone know a good solution for this.
Than开发者_StackOverflowks.
/*---------------------Distance specification----------------*/
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;
}
}
/*---------------------Distance specification----------------*/
/*-------------Radius check starts-------------------------*/
class RadiusCheck {
var $maxLat;
var $minLat;
var $maxLong;
var $minLong;
function RadiusCheck($Latitude, $Longitude, $Miles) {
global $maxLat,$minLat,$maxLong,$minLong;
$EQUATOR_LAT_MILE = 69.172;
$maxLat = $Latitude + $Miles / $EQUATOR_LAT_MILE;
$minLat = $Latitude - ($maxLat - $Latitude);
$maxLong = $Longitude + $Miles / (cos($minLat * M_PI / 180) * $EQUATOR_LAT_MILE);
$minLong = $Longitude - ($maxLong - $Longitude);
}
function MaxLatitude() {
return $GLOBALS["maxLat"];
}
function MinLatitude() {
return $GLOBALS["minLat"];
}
function MaxLongitude() {
return $GLOBALS["maxLong"];
}
function MinLongitude() {
return $GLOBALS["minLong"];
}
}
/*-------------Radius check ends---------------------------*/
/*-------------Distance check starts-----------------------*/
class DistanceCheck {
function DistanceCheck() {
}
function Calculate(
$dblLat1,
$dblLong1,
$dblLat2,
$dblLong2
) {
$EARTH_RADIUS_MILES = 3963;
$dist = 0;
//convert degrees to radians
$dblLat1 = $dblLat1 * M_PI / 180;
$dblLong1 = $dblLong1 * M_PI / 180;
$dblLat2 = $dblLat2 * M_PI / 180;
$dblLong2 = $dblLong2 * M_PI / 180;
if ($dblLat1 != $dblLat2 || $dblLong1 != $dblLong2)
{
//the two points are not the same
$dist =
sin($dblLat1) * sin($dblLat2)
+ cos($dblLat1) * cos($dblLat2)
* cos($dblLong2 - $dblLong1);
$dist =
$EARTH_RADIUS_MILES
* (-1 * atan($dist / sqrt(1 - $dist * $dist)) + M_PI / 2);
}
return $dist;
}
}
/*-------------Distance check ends--------------------------*/
/*-------------Listing datas starts--------------------------*/
// set a default number of miles to search within
$Miles = '113';
// set the user's latitude and longitude as the one to search against
$Latitude = $getip->latitude;
$Longitude = $getip->latitude;
$zcdRadius = new RadiusCheck($Latitude,$Longitude,$Miles);
$minLat = $zcdRadius->MinLatitude();
$maxLat = $zcdRadius->MaxLatitude();
$minLong = $zcdRadius->MinLongitude();
$maxLong = $zcdRadius->MaxLongitude();
$sql = "SELECT Latitude,Longitude,TimeZone,DmaId,County,Code,ctyvalue,ctstatus, ";
$sql .= "SQRT((((69.1*(Latitude-$Latitude))*(69.1*(Latitude-$Latitude)))+((53*(Longitude-$Longitude))*(53*(Longitude-$Longitude))))) ";
$sql .= "AS calc FROM geo_cities where ";
$sql .= "latitude >= '$minLat' ";
$sql .= "AND Latitude <= '$maxLat' ";
$sql .= "AND Longitude >= '$minLong' ";
$sql .= "AND Longitude <= '$maxLong' ";
//echo $sql;
$get_data = mysql_query($sql);
// loop through the matching database results
while($storedata = mysql_fetch_assoc($get_data)) {
// calculate the number of miles away the result is
$zcdDistance = new DistanceCheck;
$Distance = $zcdDistance->Calculate($Latitude,$Longitude,$storedata['latitude'],$storedata['longitude']);
// and for the non-US people, here's the km calculation
$calc_km = round(($Distance * 1.609344),2);
//echo distance($Latitude,$Longitude,$storedata['latitude'],$storedata['longitude'], "m") . " miles";
echo '<li>'.$storedata['ctyname'].'<br />'.$storedata['County'].', ';
echo 'Distance: '.$Distance.' ('.$calc_km.' km)';
}
/*-------------Listing datas ends----------------------------*/
First of all read about PHP GeoIP. As data source you can use maxmind.com databases. For instance GeoLite City (it's free).
In addition to the maxmind system, there's also ipinfodb. However IME, I wouldn't expect an average accuracy of better than 300 miles.
HTML5 provides a javascript API to determine location on the client by various methods. Where available, this is much more accurate - but it does result in a prompt to the user to permit capture of the data - and if you've no GPS / Wifi connectivity, it will just timeout.
精彩评论