PHP计算两个经纬度之间的距离

<?php

class Location
{
    public function __construct()
    {
        $this->_geo_conf = array(
            'pi'           => pi(),
            'EARTH_RADIUS' => 6378.137,
        );
    }

    public function get_distance_by_geo($x, $y)
    {
        if (!is_array($x) || !is_array($y) || !isset($x['lng']) || !isset($x['lat']) || !isset($y['lng']) || !isset($y['lat'])) {
            return false;
        }
        $lat_x = $this->_angle_to_radian($x['lat']);
        $lat_y = $this->_angle_to_radian($y['lat']);

        $a = $lat_x - $lat_y;
        $b = $this->_angle_to_radian($x['lng']) - $this->_angle_to_radian($y['lng']);

        $S = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($lat_x) * cos($lat_y) * pow(sin($b / 2), 2))) * $this->_geo_conf['EARTH_RADIUS'];

        return $S;

    }

    public function get_geo_by_distance($x, $distance)
    {
        if (!is_array($x) || !isset($x['lng']) || !isset($x['lat'])) {
            return false;
        }

        $lat_a = $this->_angle_to_radian($x['lat']);

        $lng_D = asin(sin($distance / (2 * $this->_geo_conf['EARTH_RADIUS'])) / cos($lat_a)) * 2;

        $lat_D = asin(sin($distance / (2 * $this->_geo_conf['EARTH_RADIUS']))) * 2;

        $lat_D = abs($lat_D * 180 / $this->_geo_conf['pi']);
        $lng_D = abs($lng_D * 180 / $this->_geo_conf['pi']);

        return array($lat_D, $lng_D);
    }
    private function _angle_to_radian($d)
    {
        return $d * $this->_geo_conf['pi'] / 180.0;
    }
}
发布了226 篇原创文章 · 获赞 31 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/why444216978/article/details/104186335
今日推荐