php计算坐标距离

    public function getDistance($lng1, $lat1, $lng2, $lat2)
    {
        $EARTH_RADIUS = 6378.137;
        $radLat1 = $this->rad($lat1);
        $radLat2 = $this->rad($lat2);
        $a = $radLat1 - $radLat2;
        $b = $this->rad($lng1) - $this->rad($lng2);
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
        $s = $s * $EARTH_RADIUS * 1000;

        if ($s < 1000) return round($s, 2) . '米';
        if ($s >= 1000) return round($s / 1000, 2) . '公里';
    }

    private function rad($d)
    {
        return $d * M_PI / 180.0;
    }

 

posted @ 2021-12-28 16:37  一颗糊涂淡  阅读(148)  评论(0编辑  收藏  举报