PHP查询附近的人及其距离的实现方法

PHPz
发布: 2019-11-07 15:07:15
原创
7473 人浏览过

本文实例讲述了PHP查询附近的人及其距离的实现方法。分享给大家供大家参考,具体如下:

<?php
//获取该点周围的4个点
$distance = 1;//范围(单位千米)
$lat = 113.873643;
$lng = 22.573969;
define(&#39;EARTH_RADIUS&#39;, 6371);//地球半径,平均半径为6371km
$dlng = 2 * asin(sin($distance / (2 * EARTH_RADIUS)) / cos(deg2rad($lat)));
$dlng = rad2deg($dlng);
$dlat = $distance/EARTH_RADIUS;
$dlat = rad2deg($dlat);
$squares = array(&#39;left-top&#39;=>array(&#39;lat&#39;=>$lat + $dlat,&#39;lng&#39;=>$lng-$dlng),
        &#39;right-top&#39;=>array(&#39;lat&#39;=>$lat + $dlat, &#39;lng&#39;=>$lng + $dlng),
        &#39;left-bottom&#39;=>array(&#39;lat&#39;=>$lat - $dlat, &#39;lng&#39;=>$lng - $dlng),
        &#39;right-bottom&#39;=>array(&#39;lat&#39;=>$lat - $dlat, &#39;lng&#39;=>$lng + $dlng)
        );
print_r($squares[&#39;left-top&#39;][&#39;lat&#39;]);
//从数库查询匹配的记录
$info_sql = "select * from `A` where lat<>0 and lat>{$squares[&#39;right-bottom&#39;][&#39;lat&#39;]} and lat<{$squares[&#39;left-top&#39;][&#39;lat&#39;]} and lng>{$squares[&#39;left-top&#39;][&#39;lng&#39;]} and lng<{$squares[&#39;right-bottom&#39;][&#39;lng&#39;]} ";
//获取两点之间的距离
function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2) {
  $theta = $longitude1 - $longitude2;
  $miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
  $miles = acos($miles);
  $miles = rad2deg($miles);
  $miles = $miles * 60 * 1.1515;
  $feet = $miles * 5280;
  $yards = $feet / 3;
  $kilometers = $miles * 1.609344;
  $meters = $kilometers * 1000;
  return compact(&#39;miles&#39;,&#39;feet&#39;,&#39;yards&#39;,&#39;kilometers&#39;,&#39;meters&#39;); 
}
$point1 = array(&#39;lat&#39; => 40.770623, &#39;long&#39; => -73.964367);
$point2 = array(&#39;lat&#39; => 40.758224, &#39;long&#39; => -73.917404);
$distance = getDistanceBetweenPointsNew($point1[&#39;lat&#39;], $point1[&#39;long&#39;], $point2[&#39;lat&#39;], $point2[&#39;long&#39;]);
foreach ($distance as $unit => $value) {
  echo $unit.&#39;: &#39;.number_format($value,4).&#39;<br />&#39;;
}
?>
登录后复制
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板