# How to find distance between two points on earth • ### Question

• hi All,

I want to calculate the distance (in meters) between two points whose latitude and longitude will be entered by the user...
Need this for GPS-based app in win mo 6.0

For ex :-

Double distInMet = calDistInMet (Double lat1, Double lat2, Double lon1, Double lon2);

Is thr any google api for this ?
If not, can anyone help me out what will be the code inside the body of the above method ?

Ankur
Saturday, August 8, 2009 12:10 PM

• I don't know if it is in the google API, but there's a code sample in my power management article ( http://www.codeproject.com/KB/mobile/WiMoPower1.aspx ) called GPS Perimeter that contained a class for this.

```using System;

using System.Collections.Generic;
using System.Text;

namespace GpsPerimeter
{
public static class DistanceCalculatoor
{
public const double EarthRadiusInMiles = 3956.0;
public const double EarthRadiusInKilometers = 6367.0;
public static double ToRadian(double val) { return val * (Math.PI / 180); }
public static double ToDegree(double val) { return val * 180 / Math.PI; }

/// <summary>
/// Calculate the distance between two geocodes. Defaults to using Miles.
/// </summary>

/// <summary>
/// Calculate the distance between two geocodes.
/// </summary>
public static double CalcDistance(double lat1, double lng1, double lat2, double lng2, double radius)
{

}

public static Double Bearing(double lat1, double lng1, double lat2, double lng2)
{

{
var dLat = lat2 - lat2;
var dLon = lng2 - lng1;
var dPhi = Math.Log(Math.Tan(lat2 / 2 + Math.PI / 4) / Math.Tan(lat1 / 2 + Math.PI / 4));
var q = (Math.Abs(dLat) > 0) ? dLat / dPhi : Math.Cos(lat1);

if (Math.Abs(dLon) > Math.PI)
{
dLon = dLon > 0 ? -(2 * Math.PI - dLon) : (2 * Math.PI + dLon);
}
//var d = Math.Sqrt(dLat * dLat + q * q * dLon * dLon) * R;
var brng = ToDegree(Math.Atan2(dLon, dPhi));
return brng;
}

}
}

}
```

Joel Ivory Johnson
Saturday, August 8, 2009 12:39 PM

### All replies

• I don't know if it is in the google API, but there's a code sample in my power management article ( http://www.codeproject.com/KB/mobile/WiMoPower1.aspx ) called GPS Perimeter that contained a class for this.

```using System;

using System.Collections.Generic;
using System.Text;

namespace GpsPerimeter
{
public static class DistanceCalculatoor
{
public const double EarthRadiusInMiles = 3956.0;
public const double EarthRadiusInKilometers = 6367.0;
public static double ToRadian(double val) { return val * (Math.PI / 180); }
public static double ToDegree(double val) { return val * 180 / Math.PI; }

/// <summary>
/// Calculate the distance between two geocodes. Defaults to using Miles.
/// </summary>

/// <summary>
/// Calculate the distance between two geocodes.
/// </summary>
public static double CalcDistance(double lat1, double lng1, double lat2, double lng2, double radius)
{

}

public static Double Bearing(double lat1, double lng1, double lat2, double lng2)
{

{
var dLat = lat2 - lat2;
var dLon = lng2 - lng1;
var dPhi = Math.Log(Math.Tan(lat2 / 2 + Math.PI / 4) / Math.Tan(lat1 / 2 + Math.PI / 4));
var q = (Math.Abs(dLat) > 0) ? dLat / dPhi : Math.Cos(lat1);

if (Math.Abs(dLon) > Math.PI)
{
dLon = dLon > 0 ? -(2 * Math.PI - dLon) : (2 * Math.PI + dLon);
}
//var d = Math.Sqrt(dLat * dLat + q * q * dLon * dLon) * R;
var brng = ToDegree(Math.Atan2(dLon, dPhi));
return brng;
}

}
}

}
```

Joel Ivory Johnson
Saturday, August 8, 2009 12:39 PM
• hi Joel,

Thank you for the gr8 help. I got my work done...

Thanks,
Ankur
Monday, August 10, 2009 9:41 AM