Assignment for Location Calculation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

44 lines
1.2 KiB

using System;
namespace POCDistance.Util
{
public static class CalculateDistance
{
static double toRadians(
double angleIn10thofaDegree)
{
// Angle in 10th
// of a degree
return (angleIn10thofaDegree * Math.PI) / 180;
}
public static double GetDistance(double lat1, double lon1, double lat2, double lon2)
{
// The math module contains
// a function named toRadians
// which converts from degrees
// to radians.
lon1 = toRadians(lon1);
lon2 = toRadians(lon2);
lat1 = toRadians(lat1);
lat2 = toRadians(lat2);
// Haversine formula
double dlon = lon2 - lon1;
double dlat = lat2 - lat1;
double a = Math.Pow(Math.Sin(dlat / 2), 2) +
Math.Cos(lat1) * Math.Cos(lat2) *
Math.Pow(Math.Sin(dlon / 2), 2);
double c = 2 * Math.Asin(Math.Sqrt(a));
// Radius of earth in
// kilometers. Use 3956
// for miles
double r = 6371;
// calculate the result
return (c * r);
}
}
}