title: Calculate the distance between two Geographical Coordinates in Java
body: <p>I have two GPS Coordinates, for example: <code>(44.40239182909422, 8.930511474608954)</code> and <code>(30.297017883371236, 122.3822021484364)</code></p>
  
  <p>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.</p>
  
  <p>I am trying to understand and modify this code example: </p>
  
  <pre><code> 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);
  }
  </code></pre>
  
  <p>To calculate the 'theta' I added the following code:</p>
  
  <pre><code>double theta = lon1 - lon2;
  
  if(lon2&gt;lon1)
      theta = lon2 - lon1;
  </code></pre>