在 PHP 中查找位置或附近位置

发布于 2024-10-16 04:41:14 字数 104 浏览 5 评论 0原文

大家好,我想知道是否有一个脚本可以让我使用 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.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只为守护你 2024-10-23 04:41:14
/*---------------------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----------------------------*/
/*---------------------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----------------------------*/
你另情深 2024-10-23 04:41:14

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

First of all read about PHP GeoIP. As data source you can use maxmind.com databases. For instance GeoLite City (it's free).

誰認得朕 2024-10-23 04:41:14

除了 maxmind 系统之外,还有 ipinfodb。然而 IME,我预计平均准确度不会超过 300 英里。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文