title: Find distance in meters using latitude and longitude of two different point in android
body: <p>I'm working on finding a distance between 2 points using <code>lat</code> and <code>long</code>.</p>
  
  <p>I have followed these codes.</p>
  
  <pre><code>    /* 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;
      return (dist);*/
  
      Log.e("distancefinder","Past   lat: "+lat1);
      Log.e("distancefinder","Past   long: "+lon1);
      Log.e("distancefinder","current lat : "+lat2);
      Log.e("distancefinder","current long : "+lon2);
  
      int R = 6371; // km
      double dLat = toRadians(lat2-lat1);
      double dLon = toRadians(lon2-lon1);
      lat1 = toRadians(lat1);
      lat2 = toRadians(lat2);
  
      double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
              Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
      double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
      return R * c;
  
      /*      double earthRadius = 6371000; //meters
  
      double dLat = Math.toRadians(lat2-lat1);
      double dLng = Math.toRadians(lon2-lon1);
      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;*/
  
  
      /*      double distance;
      Location locationA = new Location("");
      locationA.setLatitude(lat1);
      locationA.setLongitude(lon1);
      Location locationB = new Location("");
      locationB.setLatitude(lat2);
      locationB.setLongitude(lon2);
      distance = locationA.distanceTo(locationB);   //in meters
      return distance;*/
      //      distance = locationA.distanceTo(locationB)/1000;   //in km
  </code></pre>
  
  <p>But I didn't get accurate distance in meters. Here is my log cat results :</p>
  
  <pre><code>11-20 12:28:15.621: E/distancefinder(5309): compare lat and long
  11-20 12:28:15.621: E/distancefinder(5309): Same latitude and longtitude and return false
  11-20 12:28:25.384: E/distancefinder(5592): compare lat and long
  11-20 12:28:25.384: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:28:30.380: E/distancefinder(5592): compare lat and long
  11-20 12:28:30.381: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:28:35.404: E/distancefinder(5592): compare lat and long
  11-20 12:28:35.404: E/distancefinder(5592): Past   lat: 12.9876692
  11-20 12:28:35.404: E/distancefinder(5592): Past   long: 77.5950608
  11-20 12:28:35.404: E/distancefinder(5592): current lat : 12.9876766
  11-20 12:28:35.404: E/distancefinder(5592): current long : 77.5950719
  11-20 12:28:35.404: E/distancefinder(5592): Distance in km :0.0014572341093100844
  11-20 12:28:35.417: E/distancefinder(5592):  Distance in mts : 1.4572341093100845
  11-20 12:28:40.399: E/distancefinder(5592): compare lat and long
  11-20 12:28:40.399: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:28:45.403: E/distancefinder(5592): compare lat and long
  11-20 12:28:45.403: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:28:50.412: E/distancefinder(5592): compare lat and long
  11-20 12:28:50.412: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:28:55.398: E/distancefinder(5592): compare lat and long
  11-20 12:28:55.398: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:29:00.381: E/distancefinder(5592): compare lat and long
  11-20 12:29:00.381: E/distancefinder(5592): Past   lat: 12.9876766
  11-20 12:29:00.381: E/distancefinder(5592): Past   long: 77.5950719
  11-20 12:29:00.381: E/distancefinder(5592): current lat : 12.9876807
  11-20 12:29:00.381: E/distancefinder(5592): current long : 77.5950323
  11-20 12:29:00.382: E/distancefinder(5592): Distance in km :0.004314827685750207
  11-20 12:29:00.387: E/distancefinder(5592):  Distance in mts : 4.314827685750207
  11-20 12:29:05.407: E/distancefinder(5592): compare lat and long
  11-20 12:29:05.407: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:29:10.408: E/distancefinder(5592): compare lat and long
  11-20 12:29:10.408: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:29:15.401: E/distancefinder(5592): compare lat and long
  11-20 12:29:15.401: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:29:20.405: E/distancefinder(5592): compare lat and long
  11-20 12:29:20.405: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:29:25.386: E/distancefinder(5592): compare lat and long
  11-20 12:29:25.386: E/distancefinder(5592): Past   lat: 12.9876807
  11-20 12:29:25.386: E/distancefinder(5592): Past   long: 77.5950323
  11-20 12:29:25.386: E/distancefinder(5592): current lat : 12.9876729
  11-20 12:29:25.386: E/distancefinder(5592): current long : 77.5950339
  11-20 12:29:25.387: E/distancefinder(5592): Distance in km :8.844764712143432E-4
  11-20 12:29:25.396: E/distancefinder(5592):  Distance in mts : 0.8844764712143431
  11-20 12:29:30.392: E/distancefinder(5592): compare lat and long
  11-20 12:29:30.392: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:29:35.393: E/distancefinder(5592): compare lat and long
  11-20 12:29:35.393: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:29:40.401: E/distancefinder(5592): compare lat and long
  11-20 12:29:40.402: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:29:45.386: E/distancefinder(5592): compare lat and long
  11-20 12:29:45.386: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:29:50.403: E/distancefinder(5592): compare lat and long
  11-20 12:29:50.403: E/distancefinder(5592): Past   lat: 12.9876729
  11-20 12:29:50.403: E/distancefinder(5592): Past   long: 77.5950339
  11-20 12:29:50.403: E/distancefinder(5592): current lat : 12.9876932
  11-20 12:29:50.403: E/distancefinder(5592): current long : 77.5950936
  11-20 12:29:50.403: E/distancefinder(5592): Distance in km :0.0068510533659564225
  11-20 12:29:50.418: E/distancefinder(5592):  Distance in mts : 6.851053365956423
  11-20 12:29:55.404: E/distancefinder(5592): compare lat and long
  11-20 12:29:55.404: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:30:00.401: E/distancefinder(5592): compare lat and long
  11-20 12:30:00.401: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:30:05.381: E/distancefinder(5592): compare lat and long
  11-20 12:30:05.381: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:30:10.419: E/distancefinder(5592): compare lat and long
  11-20 12:30:10.419: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:30:15.386: E/distancefinder(5592): compare lat and long
  11-20 12:30:15.387: E/distancefinder(5592): Past   lat: 12.9876932
  11-20 12:30:15.387: E/distancefinder(5592): Past   long: 77.5950936
  11-20 12:30:15.387: E/distancefinder(5592): current lat : 12.9876918
  11-20 12:30:15.387: E/distancefinder(5592): current long : 77.5950864
  11-20 12:30:15.387: E/distancefinder(5592): Distance in km :7.955033100235504E-4
  11-20 12:30:15.394: E/distancefinder(5592):  Distance in mts : 0.7955033100235503
  11-20 12:30:20.373: E/distancefinder(5592): compare lat and long
  11-20 12:30:20.373: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:30:25.401: E/distancefinder(5592): compare lat and long
  11-20 12:30:25.401: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:30:30.411: E/distancefinder(5592): compare lat and long
  11-20 12:30:30.411: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:30:35.391: E/distancefinder(5592): compare lat and long
  11-20 12:30:35.391: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:30:40.383: E/distancefinder(5592): compare lat and long
  11-20 12:30:40.384: E/distancefinder(5592): Past   lat: 12.9876918
  11-20 12:30:40.384: E/distancefinder(5592): Past   long: 77.5950864
  11-20 12:30:40.384: E/distancefinder(5592): current lat : 12.9876826
  11-20 12:30:40.384: E/distancefinder(5592): current long : 77.5950311
  11-20 12:30:40.384: E/distancefinder(5592): Distance in km :0.006078478068578999
  11-20 12:30:40.392: E/distancefinder(5592):  Distance in mts : 6.078478068578999
  11-20 12:30:45.532: E/distancefinder(5592): compare lat and long
  11-20 12:30:45.532: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:30:50.402: E/distancefinder(5592): compare lat and long
  11-20 12:30:50.402: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:30:55.412: E/distancefinder(5592): compare lat and long
  11-20 12:30:55.412: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:31:00.409: E/distancefinder(5592): compare lat and long
  11-20 12:31:00.409: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:31:05.387: E/distancefinder(5592): compare lat and long
  11-20 12:31:05.387: E/distancefinder(5592): Past   lat: 12.9876826
  11-20 12:31:05.387: E/distancefinder(5592): Past   long: 77.5950311
  11-20 12:31:05.387: E/distancefinder(5592): current lat : 12.987683
  11-20 12:31:05.387: E/distancefinder(5592): current long : 77.5950143
  11-20 12:31:05.388: E/distancefinder(5592): Distance in km :0.0018208297478854096
  11-20 12:31:05.395: E/distancefinder(5592):  Distance in mts : 1.8208297478854096
  11-20 12:31:10.399: E/distancefinder(5592): compare lat and long
  11-20 12:31:10.399: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:31:15.405: E/distancefinder(5592): compare lat and long
  11-20 12:31:15.405: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:31:20.405: E/distancefinder(5592): compare lat and long
  11-20 12:31:20.405: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:31:25.411: E/distancefinder(5592): compare lat and long
  11-20 12:31:25.418: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:31:30.409: E/distancefinder(5592): compare lat and long
  11-20 12:31:30.409: E/distancefinder(5592): Past   lat: 12.987683
  11-20 12:31:30.409: E/distancefinder(5592): Past   long: 77.5950143
  11-20 12:31:30.409: E/distancefinder(5592): current lat : 12.9876792
  11-20 12:31:30.409: E/distancefinder(5592): current long : 77.5950169
  11-20 12:31:30.409: E/distancefinder(5592): Distance in km :5.078402770198048E-4
  11-20 12:31:30.423: E/distancefinder(5592):  Distance in mts : 0.5078402770198048
  11-20 12:31:35.410: E/distancefinder(5592): compare lat and long
  11-20 12:31:35.410: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:31:40.405: E/distancefinder(5592): compare lat and long
  11-20 12:31:40.405: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:31:45.395: E/distancefinder(5592): compare lat and long
  11-20 12:31:45.395: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:31:50.407: E/distancefinder(5592): compare lat and long
  11-20 12:31:50.407: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:31:55.399: E/distancefinder(5592): compare lat and long
  11-20 12:31:55.399: E/distancefinder(5592): Past   lat: 12.9876792
  11-20 12:31:55.399: E/distancefinder(5592): Past   long: 77.5950169
  11-20 12:31:55.399: E/distancefinder(5592): current lat : 12.9876777
  11-20 12:31:55.399: E/distancefinder(5592): current long : 77.5950228
  11-20 12:31:55.399: E/distancefinder(5592): Distance in km :6.606681020544278E-4
  11-20 12:31:55.414: E/distancefinder(5592):  Distance in mts : 0.6606681020544278
  11-20 12:32:00.404: E/distancefinder(5592): compare lat and long
  11-20 12:32:00.404: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:32:05.404: E/distancefinder(5592): compare lat and long
  11-20 12:32:05.404: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:32:10.418: E/distancefinder(5592): compare lat and long
  11-20 12:32:10.418: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:32:15.414: E/distancefinder(5592): compare lat and long
  11-20 12:32:15.414: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:32:20.392: E/distancefinder(5592): compare lat and long
  11-20 12:32:20.392: E/distancefinder(5592): Past   lat: 12.9876777
  11-20 12:32:20.392: E/distancefinder(5592): Past   long: 77.5950228
  11-20 12:32:20.392: E/distancefinder(5592): current lat : 12.9876642
  11-20 12:32:20.392: E/distancefinder(5592): current long : 77.5950319
  11-20 12:32:20.393: E/distancefinder(5592): Distance in km :0.0017959869680722845
  11-20 12:32:20.403: E/distancefinder(5592):  Distance in mts : 1.7959869680722844
  11-20 12:32:25.410: E/distancefinder(5592): compare lat and long
  11-20 12:32:25.410: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:32:30.409: E/distancefinder(5592): compare lat and long
  11-20 12:32:30.410: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:32:35.399: E/distancefinder(5592): compare lat and long
  11-20 12:32:35.399: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:32:40.403: E/distancefinder(5592): compare lat and long
  11-20 12:32:40.403: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:32:45.412: E/distancefinder(5592): compare lat and long
  11-20 12:32:45.412: E/distancefinder(5592): Past   lat: 12.9876642
  11-20 12:32:45.412: E/distancefinder(5592): Past   long: 77.5950319
  11-20 12:32:45.412: E/distancefinder(5592): current lat : 12.9876828
  11-20 12:32:45.412: E/distancefinder(5592): current long : 77.5950043
  11-20 12:32:45.413: E/distancefinder(5592): Distance in km :0.0036359967167283705
  11-20 12:32:45.425: E/distancefinder(5592):  Distance in mts : 3.6359967167283704
  11-20 12:32:50.435: E/distancefinder(5592): compare lat and long
  11-20 12:32:50.435: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:32:55.420: E/distancefinder(5592): compare lat and long
  11-20 12:32:55.420: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:33:00.405: E/distancefinder(5592): compare lat and long
  11-20 12:33:00.405: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:33:05.407: E/distancefinder(5592): compare lat and long
  11-20 12:33:05.407: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:33:10.410: E/distancefinder(5592): compare lat and long
  11-20 12:33:10.411: E/distancefinder(5592): Past   lat: 12.9876828
  11-20 12:33:10.411: E/distancefinder(5592): Past   long: 77.5950043
  11-20 12:33:10.411: E/distancefinder(5592): current lat : 12.9876684
  11-20 12:33:10.411: E/distancefinder(5592): current long : 77.5950334
  11-20 12:33:10.411: E/distancefinder(5592): Distance in km :0.0035362761324678593
  11-20 12:33:10.419: E/distancefinder(5592):  Distance in mts : 3.5362761324678593
  11-20 12:33:15.411: E/distancefinder(5592): compare lat and long
  11-20 12:33:15.411: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:33:20.392: E/distancefinder(5592): compare lat and long
  11-20 12:33:20.393: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:33:25.408: E/distancefinder(5592): compare lat and long
  11-20 12:33:25.408: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:33:30.427: E/distancefinder(5592): compare lat and long
  11-20 12:33:30.427: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:33:35.387: E/distancefinder(5592): compare lat and long
  11-20 12:33:35.388: E/distancefinder(5592): Past   lat: 12.9876684
  11-20 12:33:35.388: E/distancefinder(5592): Past   long: 77.5950334
  11-20 12:33:35.388: E/distancefinder(5592): current lat : 12.9876785
  11-20 12:33:35.388: E/distancefinder(5592): current long : 77.5950269
  11-20 12:33:35.388: E/distancefinder(5592): Distance in km :0.0013256282494112776
  11-20 12:33:35.397: E/distancefinder(5592):  Distance in mts : 1.3256282494112777
  11-20 12:33:40.408: E/distancefinder(5592): compare lat and long
  11-20 12:33:40.408: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:33:45.410: E/distancefinder(5592): compare lat and long
  11-20 12:33:45.410: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:33:50.413: E/distancefinder(5592): compare lat and long
  11-20 12:33:50.413: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:33:55.416: E/distancefinder(5592): compare lat and long
  11-20 12:33:55.416: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:34:00.412: E/distancefinder(5592): compare lat and long
  11-20 12:34:00.412: E/distancefinder(5592): Past   lat: 12.9876785
  11-20 12:34:00.412: E/distancefinder(5592): Past   long: 77.5950269
  11-20 12:34:00.414: E/distancefinder(5592): current lat : 12.9876793
  11-20 12:34:00.414: E/distancefinder(5592): current long : 77.595028
  11-20 12:34:00.414: E/distancefinder(5592): Distance in km :1.4872230719746918E-4
  11-20 12:34:00.425: E/distancefinder(5592):  Distance in mts : 0.14872230719746918
  11-20 12:34:05.417: E/distancefinder(5592): compare lat and long
  11-20 12:34:05.417: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:34:10.408: E/distancefinder(5592): compare lat and long
  11-20 12:34:10.410: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:34:15.411: E/distancefinder(5592): compare lat and long
  11-20 12:34:15.411: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:34:20.408: E/distancefinder(5592): compare lat and long
  11-20 12:34:20.408: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:34:25.388: E/distancefinder(5592): compare lat and long
  11-20 12:34:25.388: E/distancefinder(5592): Past   lat: 12.9876793
  11-20 12:34:25.388: E/distancefinder(5592): Past   long: 77.595028
  11-20 12:34:25.388: E/distancefinder(5592): current lat : 12.9876759
  11-20 12:34:25.388: E/distancefinder(5592): current long : 77.5950243
  11-20 12:34:25.388: E/distancefinder(5592): Distance in km :5.510439055217952E-4
  11-20 12:34:25.395: E/distancefinder(5592):  Distance in mts : 0.5510439055217952
  11-20 12:34:30.415: E/distancefinder(5592): compare lat and long
  11-20 12:34:30.415: E/distancefinder(5592): Same latitude and longtitude and return false
  11-20 12:34:35.412: E/distancefinder(5592): compare lat and long
  11-20 12:34:35.412: E/distancefinder(5592): Same latitude and longtitude and return false
  </code></pre>
  
  <p>So, what is the solution for this. Please help me in solving this problem.</p>