3

I need to calculate the difference between longitude and latitude of current position
and previous position. but it display the result in Exponential format and I need it in
metres. I don't understand how to convert it in metre format. Which formula require for that?

double distance2 = distanceCalculate(lat,lng,locationB.getLatitude(),locationB.getLongitude());
Toast.makeText(this, "distance2=="+Double.valueOf(distance2).longValue() + "meter" , Toast.LENGTH_SHORT).show(); 

double ActualDistance=(Double.valueOf(distance2).longValue())/1E6;
if(ActualDistance<400)
{
    System.out.println("identical");
    Toast.makeText(this,"identical", Toast.LENGTH_LONG).show();
}
else
{
    // send sms
    SmsManager sms = SmsManager.getDefault();
}

public static float distanceCalculate (double lat1, double lng1, double lat2, double lng2 ) 
{
      double earthRadius = 3958.75;
      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));
      double dist = earthRadius * c;

      int meterConversion = 1609;

      return new Float(dist * meterConversion).floatValue();
}
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Sandip Armal Patil
  • 6,241
  • 21
  • 93
  • 160

3 Answers3

2

i find this in JavaScript i think it's can help you to understand how you can do it

Pben
  • 1,081
  • 7
  • 12
1

That's a simple one -- currently you're using the radius of the Earth in miles; simply change that for the radius in metres (per Wikipedia: 6,371,000), and remove your conversion

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
1

On this question, I found this code :

public static double distFrom(double lat1, double lng1, double lat2, double lng2) { 
  double earthRadius = 3958.75; 
  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)); 
  double dist = earthRadius * c; 

  return dist; 
} 

It's a java implementation of Haversine formula.

It will return the distance in miles. For other units change the earthRadius with http://en.wikipedia.org/wiki/Earth_radius

Community
  • 1
  • 1
Julien
  • 3,509
  • 20
  • 35
  • 1
    @sandiparmal Strange at it seems that your `distanceCalculate()` formula and the one posted here `distFrom()` by Julien are actually using the same `Haversine` formula; except that in your `distanceCalculate()`, it is converted from miles to meters. From your problem description, it looks like you have a problem in *formatting* your **exponential** output which is already in meters. If you want to display the output without the exponential term, check this http://stackoverflow.com/questions/1826065/conversion-from-exponential-form-to-decimal-in-java – ecle Mar 06 '12 at 11:50
  • The earth is not a perfect sphere while your answer assumes it is. – Steve Kuo Mar 06 '12 at 16:46