php判断两个坐标的方位角

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

01protected function getAngle($lat1, $lng1, $lat2,$lng2)
02    {
03        $x1 = $lng1;
04        $y1 = $lat1;
05        $x2 = $lng2;
06        $y2 = $lat2;
07        $pi = M_PI;
08        $w1 = $y1 / 180 * $pi;
09        $j1 = $x1 / 180 * $pi;
10        $w2 = $y2 / 180 * $pi;
11        $j2 = $x2 / 180 * $pi;
12        if ($j1 == $j2) {
13            if ($w1 > $w2)
14                return 270; // 北半球的情况,南半球忽略
15            else if ($w1 < $w2)
16                return 90;
17            else
18                return -1;// 位置完全相同
19        }
20        $ret = 4* pow(sin(($w1 - $w2) / 2), 2)- pow(
21                sin(($j1 - $j2) / 2) * (cos($w1) - cos($w2)),2);
22        $ret = sqrt($ret);
23        $temp = (sin(abs($j1 - $j2) / 2) * (cos($w1) + cos($w2)));
24        $ret = $ret / $temp;
25        $ret = atan($ret) / $pi * 180;
26        if ($j1 > $j2){ // 1为参考点坐标
27            if ($w1 > $w2)
28                $ret += 180;
29            else
30                $ret = 180 - $ret;
31        } else if ($w1 > $w2)
32            $ret = 360 - $ret;
33        return $ret;
34    }
01public function getDirection($lat1, $lng1, $lat2,$lng2) {
02    $jiaodu = $this->getAngle($lat1, $lng1, $lat2, $lng2);
03    if (($jiaodu <= 10) || ($jiaodu > 350))
04    return "东";
05    if (($jiaodu > 10) && ($jiaodu <= 80))
06    return "东北";
07    if (($jiaodu > 80) && ($jiaodu <= 100))
08    return "北";
09    if (($jiaodu > 100) && ($jiaodu <= 170))
10    return "西北";
11    if (($jiaodu > 170) && ($jiaodu <= 190))
12    return "西";
13    if (($jiaodu > 190) && ($jiaodu <= 260))
14    return "西南";
15    if (($jiaodu > 260) && ($jiaodu <= 280))
16    return "南";
17    if (($jiaodu > 280) && ($jiaodu <= 350))
18    return "东南";
19    return "";
20    }

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


发表评论:

    28.73ms