1

I have two GPS Coordinates, for example: (44.40239182909422, 8.930511474608954) and (30.297017883371236, 122.3822021484364)

I would like to know the distance in meters between these two points. I don't know if the first coordinate is greater than the second or not.

I am trying to understand and modify this code example:

 private double _distance(double lat1, double lon1, double lat2, double lon2) {
      double theta = lon1 - lon2;
      double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
      dist = Math.acos(dist);
      dist = rad2deg(dist);
      dist = dist * 60 * 1.1515; // 60 is the number of minutes in a degree;  //1.1515 is the number of statute miles in a nautical mile.One nautical mile is the length of one minute of latitude at the equator.
      dist = dist * 1.609344;

      return (dist);
}

To calculate the 'theta' I added the following code:

double theta = lon1 - lon2;

if(lon2>lon1)
    theta = lon2 - lon1;
mikus
  • 3,042
  • 1
  • 30
  • 40
user880386
  • 2,737
  • 7
  • 33
  • 41

1 Answers1

2

The distance function will return the distance between two points in meteres

public double distance() {

    double lat1 = 44.40239182909422;
    double lon1 = 8.930511474608954;
    double lat2 = 30.297017883371236;
    double lon2 = 122.3822021484364;
    double theta = lon1 - lon2;
    double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
    dist = Math.acos(dist);
    dist = rad2deg(dist);
    dist = dist * 1.609344 * 1000;        
    return (dist); // 134910.69784909734
}
    /* The function to convert decimal into radians */
private double deg2rad(double deg) {
    return (deg * Math.PI / 180.0);
}       
    /* The function to convert radians into decimal */
private double rad2deg(double rad) {
    return (rad * 180.0 / Math.PI);
}
Shivam
  • 649
  • 2
  • 8
  • 20
  • the distance calculated in miles, so to convert it in KM it should multiply by 1.609344 and to convert it in Meters again multiply by 1000 – Shivam Nov 20 '15 at 13:45
  • Whewre is the earth radius hidden? – AlexWien Nov 20 '15 at 14:14
  • maybe it uses the fact that seconds are bound tightly to sea miles? and therefore the result is in miles? then the radius would be unnecessary – mikus Nov 20 '15 at 14:18
  • most probabyl. Howver this is an aporoximation. And this formula is not very good. It should not be used. There are other formulas where the earth radus can be specified, and since GPS coordinatres are related to WGS84 erath modell, the WGS84 earth radius should be used. Search for haversine formula to get a good start. – AlexWien Nov 20 '15 at 14:23
  • @AlexWien I am not sure that this formula is good or not, but I am sure that in this formula there is no need to use Radius of the Earth. The formula you are talking about is Haversine's which uses Earth's radius to calculate the distance and the link for that is this: http://stackoverflow.com/questions/3694380 – Shivam Nov 20 '15 at 14:47