views:

5567

answers:

7

Does anyone know of any free utility jars for java that assist with longitude/latitude manipulation in Java?

I am wanting to find the distance between two different points. This I know can be accomplished with the great circle distance. http://www.meridianworlddata.com/Distance-calculation.asp

Also, given a point and a distance I would like to find the point that distance north, and that distance east in order to create a box around the point.

+3  A: 

A quick Google search turns up GeoTools, which likely has the kind of functions you are looking for.

Greg Hewgill
+8  A: 

We've had some success using OpenMap to plot a lot of positional data. There's a LatLonPoint class that has some basic functionality, including distance.

bcash
Warning to possible adopters: I just ran into a BIG problem with OpenMap; they use floats internally for decimal lat/lon, which limits the accuracy depending how close to the equator you are. They plan to support the option for doubles with their OMGraphic classes starting in version 4.7, but current stable is only 4.6.5 (as of March 2010).Source: http://openmap.bbn.com/mailArchives/openmap-users/2006-01/4522.html
Marc
A: 

We use Jcoord.

Chris
+8  A: 

Here is a Java implementation of Haversine formula. I use this in a project to calculate distance in miles between lat/longs.

  public static float distFrom(float lat1, float lng1, float lat2, float lng2) {
    double earthRadius = 3958.75;
    double dLat = Math.toRadians(lat2-lat1);
    double dLng = Math.toRadians(lng2-lng1);
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
               Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
               Math.sin(dLng/2) * Math.sin(dLng/2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double dist = earthRadius * c;

    return new Float(dist).floatValue();
    }
Sean
Just a note on this, it will return the distance in miles (because of the earthRadius setting). For other units change the earthRadius (see http://en.wikipedia.org/wiki/Earth_radius for more)
John Meagher
A: 

If you use Jcoord, beware the commercial licensing restrictions

John K
A: 

Or you could use SimpleLatLng. Apache 2.0 licensed and used in one production system that I know of: mine.

Short story:

I was searching for a simple geo library and couldn't find one to fit my needs. And who wants to write and test and debug these little geo tools over and over again in every application? There's got to be a better way!

So SimpleLatLng was born as a way to store latitude-longitude data, do distance calculations, and create shaped boundaries.

I know I'm two years too late to help the original poster, but my aim is to help the people like me who find this question in a search. I would love to have some people use it and contribute to the testing and vision of this little lightweight utility.

JavadocMD