Calculate distance in meters when you know longitude and latitude in java

Possible duplicate:
Working with latitude / longitude values โ€‹โ€‹in Java

Duplicate:

  • Working with latitude / longitude values โ€‹โ€‹in Java
  • How to calculate the distance between two points of longitude latitude?

I need to calculate the distance between two points given by two coordinates. The project I'm working on is a Java project, so the Java code will be great, but the pseudocode can also be set, then I can implement it myself :)

As you probably know, there are three ways to represent coordinates:

  • Degrees: minutes: seconds (49 ยฐ 30'00 "N, 123 ยฐ 30'00" W)
  • Degrees: decimal minutes (49 ยฐ 30.0 ', -123 ยฐ 30.0'), (49d30.0m, -123d30.0 ')
  • Decimal degrees (49.5000 ยฐ, -123.5000 ยฐ), usually with 4-6 decimal numbers.

This is the third way my coordinates are indicated, so the code for these values โ€‹โ€‹will be preferable :)

+70
java latitude-longitude distance
May 08 '09 at 1:38 a.m.
source share
3 answers

Based on https://stackoverflow.com/a/312960/1/ , I got this code .. This calculates the result in meters, not miles :)

public static float distFrom(float lat1, float lng1, float lat2, float lng2) { double earthRadius = 6371000; //meters 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)); float dist = (float) (earthRadius * c); return dist; } 
+164
May 08 '09 at 2:11
source share

You can use Java Geodesy Library for GPS , it uses formulas

+18
Dec 12 '11 at 1:26
source share

In C ++, this is done as follows:

 #define LOCAL_PI 3.1415926535897932385 double ToRadians(double degrees) { double radians = degrees * LOCAL_PI / 180; return radians; } double DirectDistance(double lat1, double lng1, double lat2, double lng2) { double earthRadius = 3958.75; double dLat = ToRadians(lat2-lat1); double dLng = ToRadians(lng2-lng1); double a = sin(dLat/2) * sin(dLat/2) + cos(ToRadians(lat1)) * cos(ToRadians(lat2)) * sin(dLng/2) * sin(dLng/2); double c = 2 * atan2(sqrt(a), sqrt(1-a)); double dist = earthRadius * c; double meterConversion = 1609.00; return dist * meterConversion; } 
+4
Dec 10 '09 at 1:58 p.m.
source share



All Articles