The simplest way to calculate the distance between two longitudes and latitudes in Java

Posted by buceta on Sun, 08 Dec 2019 17:16:00 +0100

In development, we often encounter the scenario of calculating the distance between two points (longitude and latitude) or calculating the nearest store. Here is a simple implementation of how to calculate the distance between two longitudes and latitudes.

1. Import geodesy's maven dependency or download the jar package from Alibaba cloud's maven warehouse

<dependency>
  <groupId>org.gavaghan</groupId>
  <artifactId>geodesy</artifactId>
  <version>1.1.3</version>
</dependency>

 

2. Calculation

package com.test.gps;

import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

public class CaculateDistanceTest
{
    public static void main(String[] args)
    {
        GlobalCoordinates source = new GlobalCoordinates(29.490295, 106.486654);
        GlobalCoordinates target = new GlobalCoordinates(29.615467, 106.581515);

        double meter = getDistanceMeter(source, target);
        System.out.println(meter + "rice");
    }

    public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo)
    {
        //Choose the right coordinate system, choose outside Europe WGS84 Coordinate system
        Ellipsoid ellipsoid;
        if (!isPointInEurope(gpsFrom) && !isPointInEurope(gpsTo))
        {
            ellipsoid = Ellipsoid.WGS84;
        }
        else
        {
            ellipsoid = Ellipsoid.GRS80;
        }
        //Establish GeodeticCalculator,Input coordinate system, longitude and latitude for distance calculation
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);

        return geoCurve.getEllipsoidalDistance();
    }

    public static boolean isPointInEurope(GlobalCoordinates point)
    {
        try
        {
            double northernmostPoint = dms2Decimal(new int[]{ 81, 48, 24 });
            double southernmostPoint = dms2Decimal(new int[]{ 34, 48, 2 });
            double westernmostPoint = dms2Decimal(new int[]{ -24, 32, 3 });
            double easternmostPoint = dms2Decimal(new int[]{ 69, 2, 0 });
            return northernmostPoint > point.getLatitude() && southernmostPoint < point.getLatitude() && westernmostPoint < point
                    .getLongitude() && easternmostPoint > point.getLongitude();
        }
        catch (RuntimeException e)
        {
            throw new RuntimeException(e);
        }
    }

    public static double dms2Decimal(int[] dms) throws RuntimeException
    {
        if (dms != null && dms.length == 3)
        {
            int sign = dms[0] > 0 ? 1 : -1;
            double decimal = 0.0D;
            decimal += (double) Math.abs(dms[0]);
            double secondsTotal = (double) (dms[1] * 60 + dms[2]);
            decimal += secondsTotal / 3600.0D;
            return truncateDecimal(decimal) * (double) sign;
        }
        else
        {
            throw new RuntimeException();
        }
    }

    public static double truncateDecimal(double decimal)
    {
        double factor = Math.pow(10.0D, 6.0D);
        return Math.rint(decimal * factor) / factor;
    }
}

3. Output results:

Compared with the results of Baidu map, there are tens of meters of error, which can be satisfied for general application scenarios.

Topics: Java Maven