Here you can find the source of getDistance(BigDecimal sourceLatitude, BigDecimal sourceLongitude, BigDecimal destLatitude, BigDecimal destLongitude)
Parameter | Description |
---|---|
sourceLat | a parameter |
sourceLon | a parameter |
destLat | a parameter |
destLon | a parameter |
public static double getDistance(BigDecimal sourceLatitude, BigDecimal sourceLongitude, BigDecimal destLatitude, BigDecimal destLongitude)
//package com.java2s; //License from project: Apache License import java.math.BigDecimal; public class Main { private static final double KM_TO_MILE = 0.622; private static final int RADIUS = 6371; /**//from ww w. jav a 2 s. co m * This method takes the source latitude, longitude and destination latitude, longitude to * calculate the distance between two points and returns the distance * * @param sourceLat * @param sourceLon * @param destLat * @param destLon * @return double */ public static double getDistance(BigDecimal sourceLatitude, BigDecimal sourceLongitude, BigDecimal destLatitude, BigDecimal destLongitude) { double distance = 0.0; double diffOfLat = Math.toRadians(destLatitude.doubleValue() - sourceLatitude.doubleValue()); double diffOfLon = Math.toRadians(destLongitude.doubleValue() - sourceLongitude.doubleValue()); double sourceLatRad = Math.toRadians(sourceLatitude.doubleValue()); double destLatRad = Math.toRadians(destLatitude.doubleValue()); double calcResult = Math.sin(diffOfLat / 2) * Math.sin(diffOfLat / 2) + Math.cos(sourceLatRad) * Math.cos(destLatRad) * Math.sin(diffOfLon / 2) * Math.sin(diffOfLon / 2); calcResult = 2 * Math.atan2(Math.sqrt(calcResult), Math.sqrt(1 - calcResult)); distance = RADIUS * calcResult; // Converting from kms to Miles distance = distance * KM_TO_MILE; // Rounding to one decimal place distance = Math.floor(distance * 10) / 10.0; return distance; } }