您的位置:首页 > 编程语言 > PHP开发

php 计算3公里内 用户的距离

2015-10-20 10:15 495 查看
[php] view
plaincopy

/**

* 计算3公里范围内的用户

* @param type $lng string 经度

* @param type $lat string 维度

* @param type $keyword

* @return type

*/

public function actionNearUserlist()

{

$lng = $_GET['lng'];

$lat = $_GET['lat'];

$keyword = $_GET['keyword'] ? $_GET['keyword'] : "";

$half = 6371;

$distance = 20; //3公里

$dlng = 2 * asin(sin($distance / (2 * $half)) / cos(deg2rad($lat)));

$dlng = rad2deg($dlng);

$dlat = $distance / $half;

$dlat = rad2deg($dlat);

$fourpoint = 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)

);

$where = "";

if ($keyword) {

$where = "and a.name like '%" . $keyword . "%'";

}

//从数据库中查询此范围内的网点

$sql = "select uid,mobile,lng,lat

from std_student

where lat!=0 and lng!=0 and lat>" . $fourpoint['right-bottom']['lat'] . " and lat<" . $fourpoint['left-top']['lat'] .

" and lng>" . $fourpoint['left-top']['lng'] . " and lng<" . $fourpoint['right-bottom']['lng'] . " {$where}";

$res = Yii::app()->db->createCommand($sql)->queryAll();

//print_r($res);

$o = new StdStudent();

$point1 = array('lat' => $lng,'long' => $lat);

$distanc = array();

foreach ($res as $k => $v) {

$distanc = $o->getTwoDistance($point1['lat'],$point1['long'],$v['lng'],$v['lat']);

$res[$k]['meters'] = $distanc['meters'];

}

//print_r($res);

if ($res) {

$this->ajaxMessage(0,'附近三公里的用户',$res);

} else

$this->ajaxMessage(-1,'附件用户查询失败');

}

[php] view
plaincopy

public function getTwoDistance($latitude1,$longitude1,$latitude2,$longitude2)

{

$theta = $longitude1 - $longitude2;

$miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));

$miles = acos($miles);

$miles = rad2deg($miles);

$miles = $miles * 60 * 1.1515;

$feet = $miles * 5280;

$yards = $feet / 3;

$kilometers = $miles * 1.609344;

$meters = $kilometers * 1000;

return compact('miles','feet','yards','kilometers','meters');

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: