# Calculating distances with PROJ4J

I am already using proj4j in my project to convert between different coordinate reference systems. (Internally I use WGS84 but some sources supply data in a different CRS.)

Now I need to calculate the distance between two points in meters and am wondering if (and how) that is possible with PROJ4J. (Just in case: my use case is limited to a maximum distance of around 100 km.)

I have come across `org.osgeo.proj4j.util.ProjectionMath#greatCircleDistance(double, double, double, double)`, but that class is not documented at all. What CRS and unit does that method expect for the latitude/longitude values? And what is the unit for the result?

This function implements the Haversine formula (see the source code here - it is indeed not documented).

The input parameters are geographical coordinates `(double lon1, double lat1, double lon2, double lat2)` in radian. It returns a coefficient which should be multiplied by the earth radius to become a distance.

So, use for example:

``````double distanceInMeters = 6378137 * ProjectionMath.greatCircleDistance(lon1 * Math.PI/180, lat1 * Math.PI/180, lon2 * Math.PI/180, lat2 * Math.PI/180);
``````

to get a distance in meters.

• Thanks! Looking at the source code, I see that lat/lon args are used as input to `Math.sin()` and `Math.cos()`. So I assume they need to be in radians, using any spherical coordinate system (including WGS84), correct? May 26, 2020 at 22:47
• Oh, yes, good point I forgot: These angles have to be in radian ! May 27, 2020 at 5:21
• Very useful reference to the source code. I got excellent results using the average WGS radius: Public Const WGSSEMIMAJOR As Double = 6378137.0 ' Equator Public Const WGSSEMIMMINOR As Double = 6356752.314245 ' Poles Public Const WGSRADIUS As Double = (WGSSEMIMAJOR + WGSSEMIMMINOR) / 2 Aug 19, 2022 at 16:20