php判断两个坐标的方位角

  作者: thtomatic  分类: php笔记   评论: [ 0 ] 条  浏览: [ 1600 ] 次

protected function getAngle($lat1, $lng1, $lat2,$lng2)
    {
        $x1 = $lng1;
        $y1 = $lat1;
        $x2 = $lng2;
        $y2 = $lat2;
        $pi = M_PI;
        $w1 = $y1 / 180 * $pi;
        $j1 = $x1 / 180 * $pi;
        $w2 = $y2 / 180 * $pi;
        $j2 = $x2 / 180 * $pi;
        if ($j1 == $j2) {
            if ($w1 > $w2)
                return 270; // 北半球的情况,南半球忽略
            else if ($w1 < $w2)
                return 90;
            else
                return -1;// 位置完全相同
        }
        $ret = 4* pow(sin(($w1 - $w2) / 2), 2)- pow(
                sin(($j1 - $j2) / 2) * (cos($w1) - cos($w2)),2);
        $ret = sqrt($ret);
        $temp = (sin(abs($j1 - $j2) / 2) * (cos($w1) + cos($w2)));
        $ret = $ret / $temp;
        $ret = atan($ret) / $pi * 180;
        if ($j1 > $j2){ // 1为参考点坐标
            if ($w1 > $w2)
                $ret += 180;
            else
                $ret = 180 - $ret;
        } else if ($w1 > $w2)
            $ret = 360 - $ret;
        return $ret;
    }
public function getDirection($lat1, $lng1, $lat2,$lng2) {
    $jiaodu = $this->getAngle($lat1, $lng1, $lat2, $lng2);
    if (($jiaodu <= 10) || ($jiaodu > 350))
    return "东";
    if (($jiaodu > 10) && ($jiaodu <= 80))
    return "东北";
    if (($jiaodu > 80) && ($jiaodu <= 100))
    return "北";
    if (($jiaodu > 100) && ($jiaodu <= 170))
    return "西北";
    if (($jiaodu > 170) && ($jiaodu <= 190))
    return "西";
    if (($jiaodu > 190) && ($jiaodu <= 260))
    return "西南";
    if (($jiaodu > 260) && ($jiaodu <= 280))
    return "南";
    if (($jiaodu > 280) && ($jiaodu <= 350))
    return "东南";
    return "";
    }

版权所有:《thtomatic》 => 《php判断两个坐标的方位角》
本文地址:https://ask.mykeji.net/phpnotes/209.html
除非注明,文章均为 《简单记录》 原创,欢迎转载!转载请注明本文地址,谢谢。


发表评论:

    33.84ms