阿神2017-04-10 17:58:252楼
PHP安装GeoIP扩展和数据库根据IP获取访客所在国家/城市/经纬度等信息
然后就可以用geoip_record_by_name($_SERVER['REMOTE_ADDR'])获取用户经纬度.
注意:geoip_record_by_name()返回的西经和南纬是负数.
5000米转成经纬度:
纬度 Latitude: 1 deg = 110852 m
经度 Longitude: 1 deg = 111320*cos(lat) m
同一经线上,相差一纬度约为 110852 米
同一纬线上,相差一经度约为 111320*cos(lat) 米 (lat为该纬线的纬度)
//以当前用户经纬度为中心,查询5000米内的其他用户
$y = 5000 / 110852; //纬度的范围
$x = 5000 / (111320*cos($lat)); //经度的范围
$sql = '
select * from user where
lat > ($lat-$y) and lat < ($lat+$y) and
lon > ($lon-$x) and lon < ($lon+$x);
';
这个范围是一个粗略的范围,后面距离计算后把超过5公里的用户筛掉即可.
根据上面查询出来的用户的经纬度,
用半正矢公式根据经纬度计算两点间距离:
function distance($lat1, $lon1, $lat2, $lon2) {
$R = 6371393; //地球平均半径,单位米
$dlat = deg2rad($lat2-$lat1);
$dlon = deg2rad($lon2-$lon1);
$a = pow(sin($dlat/2), 2) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * pow(sin($dlon/2), 2);
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
$d = $R * $c;
return round($d);
}
echo distance(0, 0, -1, 0); // 111202米