嘿伙计们,我想知道是否有一个脚本可以让我找到一个非常准确或靠近用户使用PHP的位置。
我需要在我的代码中实现这一点,所以想知道是否有人知道一个很好的解决方案。
感谢。
答案 0 :(得分:5)
/*---------------------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----------------------------*/
答案 1 :(得分:1)
首先阅读PHP GeoIP。作为数据源,您可以使用maxmind.com数据库。例如GeoLite City(它是免费的)。
答案 2 :(得分:0)
除了maxmind系统,还有ipinfodb。不过IME,我不希望平均准确度超过300英里。
HTML5提供javascript API以通过各种方法确定客户端上的位置。在可用的情况下,这更加准确 - 但它确实会提示用户允许捕获数据 - 如果您没有GPS / Wifi连接,它将会超时。