java 根據經緯度換算距離
阿新 • • 發佈:2018-11-01
public class GSUtil { private static final double EARTH_RADIUS = 6378.137; private static double rad(double d){ return d * Math.PI / 180.0; } /** * 根據經緯度算距離 * @param long1 * @param lat1 * @param long2 * @param lat2 * @return */ public static double getmeter(double long1, double lat1, double long2, double lat2) { double a, b, d, sa2, sb2; lat1 = rad(lat1); lat2 = rad(lat2); a = lat1 - lat2; b = rad(long1 - long2); sa2 = Math.sin(a / 2.0); sb2 = Math.sin(b / 2.0); d = 2 * EARTH_RADIUS * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1) * Math.cos(lat2) * sb2 * sb2)); d= d * 1000; BigDecimal bg = new BigDecimal(d).setScale(2, RoundingMode.UP); return bg.doubleValue(); } public static void main(String[] args) { System.out.println(getmeter(116.1426, 40.0422, 116.1327, 40.0421)); } }