Java BigDecimal getDistance(BigDecimal sourceLatitude, BigDecimal sourceLongitude, BigDecimal destLatitude, BigDecimal destLongitude)

Here you can find the source of getDistance(BigDecimal sourceLatitude, BigDecimal sourceLongitude, BigDecimal destLatitude, BigDecimal destLongitude)

Description

This method takes the source latitude, longitude and destination latitude, longitude to calculate the distance between two points and returns the distance

License

Apache License

Parameter

Parameter Description
sourceLat a parameter
sourceLon a parameter
destLat a parameter
destLon a parameter

Return

double

Declaration

public static double getDistance(BigDecimal sourceLatitude, BigDecimal sourceLongitude, BigDecimal destLatitude,
        BigDecimal destLongitude) 

Method Source Code

//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;
    }
}

Related

  1. getByteArrayFromBigDecimalArray(Object value)
  2. getConversionUsdBased(final String destinycode, final BigDecimal amount, final BigDecimal usdBasedBaseRate, BigDecimal usdBasedDestinyRate)
  3. getDateFromRank(BigDecimal rank)
  4. getDepositAmount(Boolean isReceipt, BigDecimal amount)
  5. getDigits(BigDecimal value)
  6. getDividedBigDecimal(final int nDividend, final int nDivisor)
  7. getDoubleBigDecimal(String amount, int afterDot)
  8. getEndTimeByManDay(Date serviceStartTime, BigDecimal manDay)
  9. getFloatingPoint(Console console, Function validator)