1

嘿伙计们,我想知道是否有一个脚本可以让我找到使用 PHP 的用户非常准确或附近的位置。

我需要在我的代码中实现这一点,所以想看看是否有人知道一个好的解决方案。

谢谢。

4

3 回答 3

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----------------------------*/
于 2011-06-20T11:10:14.953 回答
1

首先阅读有关PHP GeoIP的信息。作为数据源,您可以使用maxmind.com数据库。例如GeoLite City(它是免费的)。

于 2011-02-07T06:11:59.430 回答
0

除了 maxmind 系统,还有ipinfodb。但是 IME,我不希望平均精度超过 300 英里。

HTML5 提供了一个javascript API来通过各种方法确定客户端上的位置。在可用的情况下,这会更加准确 - 但它确实会提示用户允许捕获数据 - 如果您没有 GPS / Wifi 连接,它将超时。

于 2011-06-20T12:24:27.403 回答