How do you calculate the distance between two markers in Google maps V3? (Similar to the distanceFrom
function inV2.)
Thanks..
Here is the c# implementation of the this forumula
public class DistanceAlgorithm
{
const double PIx = 3.141592653589793;
const double RADIO = 6378.16;
///
/// This class cannot be instantiated.
///
private DistanceAlgorithm() { }
///
/// Convert degrees to Radians
///
/// Degrees
/// The equivalent in radians
public static double Radians(double x)
{
return x * PIx / 180;
}
///
/// Calculate the distance between two places.
///
///
///
///
///
///
public static double DistanceBetweenPlaces(
double lon1,
double lat1,
double lon2,
double lat2)
{
double dlon = Radians(lon2 - lon1);
double dlat = Radians(lat2 - lat1);
double a = (Math.Sin(dlat / 2) * Math.Sin(dlat / 2)) + Math.Cos(Radians(lat1)) * Math.Cos(Radians(lat2)) * (Math.Sin(dlon / 2) * Math.Sin(dlon / 2));
double angle = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
return (angle * RADIO) * 0.62137;//distance in miles
}
}