根据一点坐标和距离计算另一点坐标(PHP),以及计算两点间距离(PHP)

来源:互联网 发布:淘宝裸根月季苗 编辑:程序博客网 时间:2024/05/21 17:18

最近在开发关于地理位置的一个应用,遇到了一个问题。
现在有BD09II坐标系的坐标,想以此点为中心,获得正东、正南、正西、正北四个坐标位置,这四个位置距离提供的坐标距离此点距离相等。这样做的原因是为了能够在MySQL里搜索,取“范围域”。

不多说,上代码。
(注意,这个代码是初步版,没有考虑资源消耗或者什么设计模式什么的,希望各位复写的时候再进行改良啦~ 本人初学者。轻拍 TAT )

<?php/** * Created by PhpStorm. * User: 关大帅 * Date: 2015/11/24 0024 * Time: 17:46 */class calculator{    private $temp;//公式中的一个变量。。。    private $lat;//传入的纬度    private $lon;//传入的经度    private $r=6371000.;//地球半径    private $array;//返回array格式的数据    private $PI=3.14159265358979324; //字面意思    private $distance=500; //求500m距离//从main开始执行    public function main($lat,$lon){        //this is the main function of this class        $this->set_latlon($lat,$lon);        $this->set_temp();        if(is_null($this->temp))            return "{error-8001: 输入的数值出现错误,请您重新输入}";        else        {            $this->set_array();            return $this->array;        }    }    private function set_latlon($lat,$lon){        $this->lat=$lat;        $this->lon=$lon;    }    private function set_temp(){        $this->temp=cos($this->distance/$this->erathR/sqrt(2));     }    private function set_array(){        //1:纬北 2:纬南 3:经东 4:经西        $this->array=array("1"=>$this->cal_max_lat(),"2"=>$this->cal_min_lat(),"3"=>$this->cal_max_lon(),"4"=>$this->cal_min_lon());    }    private function cal_min_lat(){        $temp=$this->temp;        return $this->lat-(acos($temp)/($this->PI/180));    }    private function cal_max_lat(){        $temp=$this->temp;        return $this->lat+(acos($temp)/($this->PI/180));    }    private function cal_max_lon(){        $temp=$this->temp;        $temp_lon=acos( ( $temp - (sin ($this->lat * ($this->PI/180) ) )*(sin ($this->lat * ($this->PI/180) ) ) )   / ( cos ($this->lat *( $this->PI/180 ))* cos ($this->lat *( $this->PI/180 )) ))/($this->PI/180);        return $this->lon+$temp_lon;    }    private function cal_min_lon(){        $temp=$this->temp;        $temp_lon=acos( ( $temp - (sin ($this->lat * ($this->PI/180) ) )*(sin ($this->lat * ($this->PI/180) ) ) )   / ( cos ($this->lat *( $this->PI/180 ))* cos ($this->lat *( $this->PI/180 )) ))/($this->PI/180);        return $this->lon-$temp_lon;    }}$test=new calcultr();$array=$tst->main(1,1);//东部:$lon,$array[3];//北部:$array[1],$lat;//西部:$lon,$array[4];//南部:$array[2],$lat;?>

测试数据:main (1,1); 获得结果如下:1.0063591640357 0.99364083596434 1.006360132786 0.99363986721399。 OC关大帅

1 0
原创粉丝点击