計算地圖中兩點之間的距離

entaro發表於2022-01-17
public static double distance(Double long1, Double lat1, double long2, double lat2) {
        if (long1 == null || lat1 == null || long1 == 0 || lat1 == 0) {
            return 0d;
        }
        double a, b, R;
        R = 6378137; // 地球半徑
        lat1 = lat1 * Math.PI / 180.0;
        lat2 = lat2 * Math.PI / 180.0;
        a = lat1 - lat2;
        b = (long1 - long2) * Math.PI / 180.0;
        double d;
        double sa2, sb2;
        sa2 = Math.sin(a / 2.0);
        sb2 = Math.sin(b / 2.0);
        d = 2
                * R
                * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1)
                * Math.cos(lat2) * sb2 * sb2));
        return d;
    }
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章