$i = 10.0; //差值可自定义,值越大,范围就越大
$min_latitude = $lat - $i; //纬度最小值
$max_latitude = $lat + $i; //纬度最大值
$min_longitude = $lng - $i; //经度最小值
$max_longitude = $lng + $i; //经度最大值
where('(c.shop_address_x BETWEEN '.$min_latitude.' AND '.$max_latitude.') AND (c.shop_address_y BETWEEN '.$min_longitude.' AND '.$max_longitude.')')
//根据店铺距离用户的远近进行从近到远的排序
getDistance($value['shop_address_x'],$value['shop_address_y'],$lat,$lng).'km';
//根据用户经度纬度和店铺的经度纬度 计算两者之间的距离
/**
* 计算两组经纬度坐标 之间的距离
* params :lat1 纬度1; lng1 经度1; lat2 纬度2; lng2 经度2; len_type (1:m or 2:km);
* return m or km
*/
function getDistance($lat1, $lng1, $lat2, $lng2, $len_type = 2, $decimal = 2)
{
$earth_radius = 6378.137;
$pi = 3.1415926;
// define('EARTH_RADIUS', 6378.137);//地球半径
// define('PI', 3.1415926);
$radLat1 = $lat1 * $pi / 180.0;
$radLat2 = $lat2 * $pi / 180.0;
$a = $radLat1 - $radLat2;
$b = ($lng1 * $pi / 180.0) - ($lng2 * $pi / 180.0);
$s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
$s = $s * $earth_radius;
$s = round($s * 1000);
if ($len_type > 1)
{
$s /= 1000;
}
return round($s, $decimal);
}