//$distance 是距离城市的长度单位km
//$location 是城市
function returnSquarePoint($distance,$location){
/***计算某个经纬度的周围某段距离的正方形的四个点
*
*@param lng float 经度
*@param lat float 纬度
*@param distance float 该点所在圆的半径,该圆与此正方形内切,默认值为0.5千米
*@return array 正方形的四个点的经纬度坐标
*/
//如果没有$location就获取当前用户的城市,再查询出经纬度,可根据自己的情况修改
$user=M('world');
if($location==null){$city=M('common_user');
$data=$city->where("id='".session('user_id')."'")->getField('zip_or_city');
$list=$user->where("city='".$data."'")->find();
}else{
//如果有$location就查询出城市的经纬度
$list=$user->where("city='".$location."'")->find();}
if($list==null){
return $data or $location;
}else{
$lat=$list['latitude'];
$lng=$list['longitude'];
define(EARTH_RADIUS, 6371);//地球半径,平均半径为6371km
$dlng = 2 * asin(sin($distance / (2 * EARTH_RADIUS)) / cos(deg2rad($lat)));
$dlng = rad2deg($dlng);
//echo $dlng;
$dlat = $distance/EARTH_RADIUS;
$dlat = rad2deg($dlat);
$squares= 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查询
$info_sql = "select city from `cup_world` where latitude<>0 and latitude>{$squares['right-bottom']['lat']} and latitude<{$squares['left-top']['lat']} and longitude>{$squares['left-top']['lng']} and longitude<{$squares['right-bottom']['lng']} ";
$city1=$user->query($info_sql);
return $city1;
}
}