Calculate distance with JTS

The short answer is you can't do that unless your points are very close together and you want the answer in degrees. JTS knows nothing about units or the curvature of the earth. So you need to pull in some GeoTools jars that do know about such things. Then you can create a method like:

private void calculateDistance(CoordinateReferenceSystem crs, Point[] points) {
    if (crs == null) {
        crs = default_crs;
    }

    double distance = 0.0;
    try {
        distance = JTS.orthodromicDistance(
            points[0].getCoordinate(),
            points[1].getCoordinate(),
            crs
        );
    } catch (TransformException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Measure<Double, Length> dist = Measure.valueOf(distance, SI.METER);
    System.out.println(dist.doubleValue(SI.KILOMETER) + " Km");
    System.out.println(dist.doubleValue(NonSI.MILE) + " miles");
}

Which will take care of all the hard maths for you. The full example program can be seen here.


You could use the Java implementation of GeographicLib to solve the inverse geodesic problem on WGS84.

import net.sf.geographiclib.*;

...

    private double calculateDistance(
            double lon1, double lat1, double lon2, double lat2) {
        GeodesicData g = Geodesic.WGS84.Inverse(lat1, lon1, lat2, lon2);
        return g.s12;  // distance in metres
    }

The length of the geodesic, which represents the shortest distance in metres between the two points, is in g.s12.