Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I measure distance and create a bounding box based on two latitude+longitude points 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

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

like image 571
will Avatar asked Sep 23 '08 10:09

will


People also ask

How do I calculate the distance between two points specified by latitude and longitude?

For this divide the values of longitude and latitude of both the points by 180/pi. The value of pi is 22/7. The value of 180/pi is approximately 57.29577951. If we want to calculate the distance between two places in miles, use the value 3, 963, which is the radius of Earth.

Can you calculate distance based on latitude and longitude?

One of the most common ways to calculate distances using latitude and longitude is the haversine formula, which is used to measure distances on a sphere. This method uses spherical triangles and measures the sides and angles of each to calculate the distance between points.

How do you find the distance between two latitude lines?

If you treat the Earth as a sphere with a circumference of 25,000 miles, then one degree of latitude is 25,000/360 = 69.44 miles. A minute is thus 69.44/60 = 1.157 miles, and a second is 1.15/60 = 0.0193 miles, or about 101 feet.


2 Answers

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

public static double distFrom(double lat1, double lng1, double lat2, double lng2) {     double earthRadius = 3958.75; // miles (or 6371.0 kilometers)     double dLat = Math.toRadians(lat2-lat1);     double dLng = Math.toRadians(lng2-lng1);     double sindLat = Math.sin(dLat / 2);     double sindLng = Math.sin(dLng / 2);     double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)             * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));     double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));     double dist = earthRadius * c;      return dist;     } 
like image 160
Sean Avatar answered Oct 03 '22 08:10

Sean


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.

like image 45
JavadocMD Avatar answered Oct 03 '22 06:10

JavadocMD