Home > Backend Development > PHP Tutorial > PHP根据一个给定经纬度的点,进行附近地点查询?合理利用算法

PHP根据一个给定经纬度的点,进行附近地点查询?合理利用算法

WBOY
Release: 2016-06-23 13:40:58
Original
1185 people have browsed it

实现原理先算出该点周围的矩形的四个点,然后使用经纬度去直接匹配数据库中的记录。   //获取周围坐标   public function returnSquarePoint($lng, $lat,$distance = 0.5){         $earthRadius = 6378138;        $dlng =  2 * asin(sin($distance / (2 * $earthRadius)) / cos(deg2rad($lat)));        $dlng = rad2deg($dlng);        $dlat = $distance/$earthRadius;        $dlat = rad2deg($dlat);        return array(                       'left-top'=>array('lat'=>$lat + $dlat,'lng'=>$lng-$dlng),                       'right-top'=>array('lat'=>$lat + $dlat, 'lng'=>$lng + $dlng),                       'left-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng - $dlng),                       'right-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng + $dlng)        );   }   //计算两个坐标的直线距离      public function getDistance($lat1, $lng1, $lat2, $lng2){                $earthRadius = 6378138; //近似地球半径米          // 转换为弧度          $lat1 = ($lat1 * pi()) / 180;          $lng1 = ($lng1 * pi()) / 180;          $lat2 = ($lat2 * pi()) / 180;          $lng2 = ($lng2 * pi()) / 180;          // 使用半正矢公式  用尺规来计算        $calcLongitude = $lng2 - $lng1;          $calcLatitude = $lat2 - $lat1;          $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);         $stepTwo = 2 * asin(min(1, sqrt($stepOne)));          $calculatedDistance = $earthRadius * $stepTwo;          return round($calculatedDistance);   }
Copy after login

 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template