You use the wrong formula. That's the reason you don't get the correct result.
The formula you use is the one we use for the calculation of the distance between two points
in the same plane and can be proved using the pythagora's theorem. However when we want to calculate the distance between two point on the surface of a sphere (we assume that the earth is a perfect sphere), we don't use this type.
Here is a link with the correct formula and an implementation in JavaScript.
Below, I have an implementation in C#
At first we have to define a method that would take as a parameter an angle and it will return it's value in radians.
public double ConvertToRadians(double angle)
{
return (Math.PI / 180) * angle;
}
Then we could define our method for the calculation of the distance:
public static double DistanceTo(double latitude1,
double longitude1,
double latitude2,
double longitude2)
{
// The radius of the earth in Km.
// You could also use a better estimation of the radius of the earth
// using decimals digits, but you have to change then the int to double.
int R = 6371;
double f1 = ConvertToRadians(latitude1);
double f2 = ConvertToRadians(latitude2);
double df = ConvertToRadians(latitude1-latitude2);
double dl = ConvertToRadians(longitude1-longitude2);
double a = Math.Sin(dφ/2) * Math.Sin(dφ/2) +
Math.Cos(f1) * Math.Cos(f2) *
Math.Sin(dλ/2) * Math.Sin(dλ/2);
double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1-a));
// Calculate the distance.
double d = R * c;
return d;
}
If you don't want to implement it as above, you could use the GeoCoordinate
class, which
Represents a geographical location that is determined by latitude and
longitude coordinates. May also include altitude, accuracy, speed, and
course information.
If you do so, then:
var point1 = new GeoCoordinate(latitude1, longitude1);
var point2 = new GeoCoordinate(latitude2, latitude2);
and then you get the distance between point1
and point2
like below:
point1.GetDistanceTo(point2);