Calculate Nearest Latitude and Longitude From Current Lat Long In C#
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
double currentLatitude = 37.774929;
double currentLongitude = -122.419416;
List<LatitudeLongitude> latitudeLongitudes = new List<LatitudeLongitude>
{
new LatitudeLongitude
{
Latitude = 51.217611000000,
Longitude = 4.421000000000,
},
new LatitudeLongitude
{
Latitude = 50.844833000000,
Longitude = 4.355861000000
}
};
const int EARTH_RADIUS_IN_MILES = 3959;
double distanceFilter = 2.0;
List<LatitudeLongitude> filteredLatitudeLongitudes = latitudeLongitudes.FindAll(x =>
{
double latitudeDifference = x.Latitude - currentLatitude;
double longitudeDifference = x.Longitude - currentLongitude;
double a = Math.Sin(latitudeDifference / 2) * Math.Sin(latitudeDifference / 2) +
Math.Cos(currentLatitude) * Math.Cos(x.Latitude) *
Math.Sin(longitudeDifference / 2) * Math.Sin(longitudeDifference / 2);
double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
double distance = EARTH_RADIUS_IN_MILES * c;
return distance <= distanceFilter;
});
foreach (LatitudeLongitude latLong in filteredLatitudeLongitudes)
{
Console.WriteLine("Latitude: " + latLong.Latitude);
Console.WriteLine("Longitude: " + latLong.Longitude);
Console.WriteLine();
}
}
}
No comments:
Post a Comment