当前位置:Gxlcms > PHP教程 > php计算3公里内所以用户的距离

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

时间:2021-07-01 10:21:17 帮助过:2人阅读

/**
     * 计算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,'附件用户查询失败');
    }
 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');
    }

以上就介绍了php 计算3公里内所以用户的距离,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

人气教程排行