/**
* 计算某个经纬度的周围某段距离的正方形的四个点
*
* @param
* lng float 经度
* @param
* lat float 纬度
* @param
* distance float 该点所在圆的半径,该圆与此正方形内切,默认值为0.5千米
* @return array 正方形的四个点的经纬度坐标
*/
function returnSquarePoint($lng, $lat, $distance = 0.5)
{
// defined(EARTH_RADIUS) || define(EARTH_RADIUS, 6371);//地球半径,平均半径为6371km
$dlng = 2 * asin(sin($distance / (2 * 6371)) / cos(deg2rad($lat)));
$dlng = rad2deg($dlng);
$dlat = $distance / 6371;
$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
)
);
}
获取附近:
$sql = "select * from `network_office_building` where province=? and city=? and district=? and location_lng<>0 and location_lat>{$squares['right-bottom']['lat']} and location_lat<{$squares['left-top']['lat']} and location_lng>{$squares['left-top']['lng']} and location_lng<{$squares['right-bottom']['lng']} ";
$results = DB::connection('net')->select($sql, [
$row->province,
$row->city,
$row->district
]);
http://blog.51cto.com/imguowei/1952035