1.根据经纬度 查询附近信息
/**
*计算某个经纬度的周围某段距离的正方形的四个点
*
*@param lng float 经度
*@param lat float 纬度
*@param distance float 该点所在圆的半径,该圆与此正方形内切,默认值为0.5千米
*@return array 正方形的四个点的经纬度坐标
*/
private function returnSquarePoint($lng, $lat, $distance){
$distance = $distance?$distance:5;
$earthRadius = 6378;
$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)
);
}
$info_sql = "select id,locateinfo,lat,lng from `lbs_info` where lat<>0 and lat>{$squares['right-bottom']['lat']} and lat<{$squares['left-top']['lat']} and lng>{$squares['left-top']['lng']} and lng<{$squares['right-bottom']['lng']} ";
2.根据经纬度查询两点之间的距离
/**
* @desc 根据两点间的经纬度计算距离
* @param float $lat 纬度值
* @param float $lng 经度值
*/
private function getDistance($lat1, $lng1, $lat2, $lng2){
$earthRadius = 6367; //approximate radius of earth in meters
$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,2);
}
网友评论