img

通过经纬度计算出位置

2020-06-16 0条评论 2.1k次阅读 PHP


参考文章: https://blog.csdn.net/dawei5565/article/details/101760677

使用

public $lng = 0;
public $lat = 0;

if($this->lng && $this->lat){
    $this->dbCondition->addConditions([
        new AddDistanceSortCondition($this->lng,$this->lat)
    ]);

    $this->dbCondition->order('distance ASC,id DESC');
}else{
    $this->dbCondition->order('id DESC');
}

AddDistanceSortCondition.php

class AddDistanceSortCondition implements ICondition
{

    private $_lng = 0;
    private $_lat = 0;

    private $_table_lng_filed = 'lng';
    private $_table_lat_filed = 'lat';

    private $_as_distance_name = 'distance';

    public function __construct($lng,$lat,$as_distance_name = 'distance'){
        $this->_lng = $lng;
        $this->_lat = $lat;
        $this->_as_distance_name = $as_distance_name;
    }

    public function setTableLngFiled($table_lng_filed){
        $this->_table_lng_filed = $table_lng_filed;
        return $this;
    }

    public function setTableLatFiled($table_lat_filed){
        $this->_table_lat_filed = $table_lat_filed;
        return $this;
    }

    /**
     * __construct结束后就自动调到的方法
     * @param CDbConditionTrait $dbCondition
     * @param CRequest         $request
     */
    public function onBuild($dbCondition, CRequest $request)
    {
        if($this->_lng && $this->_lat){
            $distance_sql = 'round(6378.138*2*asin(sqrt(pow(sin( ('.$this->_lat.'*pi()/180-'.$this->_table_lat_filed.'*pi()/180)/2),2)+cos('.$this->_lat.'*pi()/180)*cos('.$this->_table_lat_filed.'*pi()/180)* pow(sin( ('.$this->_lng.'*pi()/180-'.$this->_table_lng_filed.'*pi()/180)/2),2)))*1000)';
            $distance_sql .= ' as '.$this->_as_distance_name;

            if($dbCondition->getSelect() === null){
                $distance_sql = '*,'.$distance_sql;
            }
            $dbCondition->addSelect($distance_sql);
        }

    }

}

打印的sql语句:

select *,round(6378.138*2*asin(sqrt(pow(sin( (30.633300*pi()/180-lat*pi()/180)/2),2)+cos(30.633300*pi()/180)*cos(lat*pi()/180)* pow(sin( (103.976061*pi()/180-lng*pi()/180)/2),2)))*1000) as distance from t_person_company as t   where 1     order by distance, id DESC  limit 0, 11

💬 COMMENT


🦄 支持markdown语法

👋友