标签:
实现原理先算出该点周围的矩形的四个点,然后使用经纬度去直接匹配数据库中的记录。 //获取周围坐标 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); }
PHP根据一个给定经纬度的点,进行附近地点查询–合理利用算法
标签:
原文地址:http://my.oschina.net/u/151223/blog/371781