Here you can find the source of calcDistance(final BigDecimal x1, final BigDecimal y1, final BigDecimal x2, final BigDecimal y2)
Parameter | Description |
---|---|
x1 | = long1 |
y1 | = lat1 |
x2 | = long2 |
y2 | = lat2 |
public static double calcDistance(final BigDecimal x1, final BigDecimal y1, final BigDecimal x2, final BigDecimal y2)
//package com.java2s; //License from project: Open Source License import java.math.BigDecimal; public class Main { /**//from w w w . j a va 2 s.c o m * Calculates distance traveled between two sequential messages. * * @param x1 * = long1 * @param y1 * = lat1 * @param x2 * = long2 * @param y2 * = lat2 * @return */ public static double calcDistance(final BigDecimal x1, final BigDecimal y1, final BigDecimal x2, final BigDecimal y2) { return calcDistance(x1.doubleValue(), y1.doubleValue(), x2.doubleValue(), y2.doubleValue()); } /** * Calculates distance traveled between two sequential messages. * * @param x1 * = long1 * @param y1 * = lat1 * @param x2 * = long2 * @param y2 * = lat2 * @return */ public static double calcDistance(final double x1, final double y1, final double x2, final double y2) { return distanceInMetersBetween(y1, x1, y2, x2); } /** * Calculates a distance between two "world" points. * * @see http://stackoverflow.com/questions/837872/calculate-distance-in-meters-when-you-know-longitude-and-latitude-in-java * * @param lat1 * @param lng1 * @param lat2 * @param lng2 * @return */ private static double distanceInMetersBetween(final double lat1, final double lng1, final double lat2, final double lng2) { final double earthRadius = 3958.75; final double dLat = Math.toRadians(lat2 - lat1); final double dLng = Math.toRadians(lng2 - lng1); final double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2) * Math.sin(dLng / 2); final double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); final double dist = earthRadius * c; final int meterConversion = 1609; return dist * meterConversion; } }