【问题标题】:Find location or near by location in PHP在 PHP 中查找位置或按位置附近
【发布时间】:2011-02-07 05:57:10
【问题描述】:

大家好,我想知道是否有一个脚本可以让我使用 PHP 找到相当准确或附近的用户位置。

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

谢谢。

【问题讨论】:

  • 取决于您所说的“非常准确”。使用 PHP,您只能进行基于 IP 地址的地理定位。这将非常准确地告诉您访问者来自哪个国家/地区。我经常遇到IP地理定位的“城市”信息有200km以上的误差...
  • 在某些浏览器(Chrome、Safari)中可以通过 JavaScript 获取更精确的信息。浏览器将使用附近的 wifi 网络来查找当前位置。但是,浏览器会在允许检索用户位置之前要求确认。

标签: php geolocation ip-geolocation


【解决方案1】:
/*---------------------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----------------------------*/

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 是的,MaxMind 数据库看起来很有前途。我想我最终可能会购买他们的付费版本。谢谢:)
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      • 2023-03-28
      • 2015-07-04
      • 2017-09-07
      相关资源
      最近更新 更多