Tag: PHP

参考にしたところ

ソース

  • だいたいの距離が出れば良いのでざっくりと
  • まだ入力値のチェックとかしてない
    <?php
    
    $geo = new Geo('dd.mm.ss.sss', 'dd.mm.ss.sss');
    echo $geo->getDistance('dd.mm.ss.sss', 'dd.mm.ss.sss'), "m\n";
    
    class Geo
    {
        private $_lat;
        private $_lon;
        private $_lat_m;
        private $_lon_m;
        
        public function __construct($lat, $lon)
        {
            $this->_setPoint($lat, $lon);
        }
    
        private function _setPoint($lat, $lon)
        {
            $this->_lat   = $this->_toDm($lat);
            $this->_lon   = $this->_toDm($lon);
            $this->_lat_m = 6378150 * 2 * pi() / 360;
            $this->_lon_m = 6378150 * cos($this->_lat / 180 * pi()) * 2 * pi() / 360;
        }
    
        private function _toDm($val)
        {
            $buf = preg_split('/[\.,]/', $val);
            return $buf[0] + ($buf[1] + $buf[2] / 60) / 60;
        }
    
        public function getDistance($lat, $lon)
        {
            $target_lat = $this->_toDm($lat);
            $target_lon = $this->_toDm($lon);
    
            $lat_d = $target_lat - $this->_lat;
            $lon_d = $target_lon - $this->_lon;
    
            //$this->_lon_m = 6378150 * cos((($this->_lat + $target_lat) / 2) / 180 * pi()) * 2 * pi() / 360;
            
            return sqrt(pow($lat_d * $this->_lat_m, 2) + pow($lon_d * $this->_lon_m, 2));
        }
    }

Last-modified: 2010-06-09 (水) 15:53:48 (2744d)