How to calculate the difference between longitudes?
I need to calculate the difference between the longitude and latitude of the current position
and the previous position. but it displays the result in exponential format and I need it in
meters. I don't understand how to convert it to counter format. What formula does this require?
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();
}
+3
source to share
3 answers
It's simple - you are currently using the Earth's radius in miles; just change the radius value in meters ( per Wikipedia : 6,371,000) and remove the conversion
+1
source to share
In 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;
}
This is a java implementation of Haversine .
It will return the distance in miles. For other units, change the earthRadius value with http://en.wikipedia.org/wiki/Earth_radius
+1
source to share