1.根據(jù)經(jīng)緯度 查詢附近信息
/**
*計算某個經(jīng)緯度的周圍某段距離的正方形的四個點
*
*@param lng float 經(jīng)度
*@param lat float 緯度
*@param distance float 該點所在圓的半徑五续,該圓與此正方形內(nèi)切洒敏,默認值為0.5千米
*@return array 正方形的四個點的經(jīng)緯度坐標
*/
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.根據(jù)經(jīng)緯度查詢兩點之間的距離
/**
* @desc 根據(jù)兩點間的經(jīng)緯度計算距離
* @param float $lat 緯度值
* @param float $lng 經(jīng)度值
*/
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);
}