根据两点经纬度计算方位角

之前找了一个方法,后来运行有点问题现在新找了了一个,留作记录

新方法:

/**
 * 计算两点之间的角度
 * @return
 */
public double getAngle(double lng1,double lat1, double lng2,double lat2) {
    double dRotateAngle = Math.atan2(Math.abs(lng1 - lng2), Math.abs(lat1 - lat2));
    if (lng2 >= lng1) {
        if (lat2 >= lat1) {
        } else {
            dRotateAngle = Math.PI - dRotateAngle;
        }
    } else {
        if (lat2 >= lat1) {
            dRotateAngle = 2 * Math.PI - dRotateAngle;
        } else {
            dRotateAngle = Math.PI + dRotateAngle;
        }
    }
    dRotateAngle = dRotateAngle * 180 / Math.PI;
    return dRotateAngle;
}

之前方法,之前方法在一次计算中出现结果为NAN的情况,没有后续继续查,先用上面的方法再说:

/**
     * 根据两点经纬度计算方位角
     */
    public double computeAzimuth(double lng01, double lat01,double lng02,double lat02) {
        double lat1 = lat01, lon1 = lng01, lat2 = lat02,
                lon2 = lng02;
        double result = 0.0;

        int ilat1 = (int) (0.50 + lat1 * 360000.0);
        int ilat2 = (int) (0.50 + lat2 * 360000.0);
        int ilon1 = (int) (0.50 + lon1 * 360000.0);
        int ilon2 = (int) (0.50 + lon2 * 360000.0);

        lat1 = Math.toRadians(lat1);
        lon1 = Math.toRadians(lon1);
        lat2 = Math.toRadians(lat2);
        lon2 = Math.toRadians(lon2);

        if ((ilat1 == ilat2) && (ilon1 == ilon2)) {
            return result;
        } else if (ilon1 == ilon2) {
            if (ilat1 > ilat2)
                result = 180.0;
        } else {
            double c = Math
                    .acos(Math.sin(lat2) * Math.sin(lat1) + Math.cos(lat2)
                            * Math.cos(lat1) * Math.cos((lon2 - lon1)));
        
            double A = Math.asin(Math.cos(lat2) * Math.sin((lon2 - lon1)) / Math.sin(c));

            result = Math.toDegrees(A);
            if ((ilat2 > ilat1) && (ilon2 > ilon1)) {
            } else if ((ilat2 < ilat1) && (ilon2 < ilon1)) {
                result = 180.0 - result;
            } else if ((ilat2 < ilat1) && (ilon2 > ilon1)) {
                result = 180.0 - result;
            } else if ((ilat2 > ilat1) && (ilon2 < ilon1)) {
                result += 360.0;
            }
        }

        if(result<0){
            result +=360.0;
        }
        if(result>360){
            result -=360.0;
        }

        return result;
    }

 

转载自:https://blog.csdn.net/u011435933/article/details/84385532

You may also like...

退出移动版