I have list of places and there latitude and longitude and i have to find out nearest places to my current location by calculating distance and then it will print on console one by one
public class Location
{
public string Place { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public Location( string Place_, double Latitude_, double Longitude_)
{
this.Place = Place_;
this.Latitude = Latitude_;
this.Longitude = Longitude_;
}
}
public static void Main() {
List<Location> locations = new List<Location>();
locations.Add(new Location( "Narayana International", 28.6075582,77.04700729999999));
locations.Add(new Location( "Digital zone", 28.8610328484855, 77.0951520020164));
locations.Add(new Location( "Balaji computers", 28.841364952771706, 77.09076910484801));
locations.Add(new Location( "Codac Infosys", 29.967759964202948, 76.88406425590289));
locations.Add(new Location( "Vijay computers", 30.6920753011206, 76.80162611916126));
locations.Add(new Location( "Aadi online exam solutions", 30.393033405609803, 76.79088732285474));
locations.Add(new Location( "Shamal online exam solutions", 31.039831347298907, 77.12095380938695));
locations.Add(new Location( "Solani online exam solutions", 30.906763881433086, 77.08748709160578));
locations.Add(new Location( "S.R.P online exam solutions", 30.68625270405994, 76.82522332445183));
locations.Add(new Location( "SCORE HIGH", 28.7022839, 77.16902390000001));
double searchLat = 19;//my loaction
double searchLong = 74;//my location
Dictionary<double, List<Location>> distances = new Dictionary<double, List<Location>>();
locations.ForEach(location => {
double distance = Program.GetDistance(location.Latitude, location.Longitude, searchLat, searchLong, location.Latitude ,location.Longitude );
if(distances.ContainsKey(distance)){
distances[distance].Add(location);
}else{
distances.Add(distance, new List<Location>() { location });
}
});
Console.WriteLine("Locations Are:" + locations.Count );
Console.WriteLine("This is the nearest location to me:");
double closestDistanceFromSearch = distances.Keys.OrderBy(k => k).First();
Console.WriteLine(distances[distances.Keys.OrderBy(k => k).First()].First().Place);
Console.ReadLine();
}
public static double GetDistance(double latitude, double longitude, double Latitude1, double Longitude1, double Latitude2, double Longitude2) {
var d1 = latitude * (Math.PI / 180.0);
var num1 = longitude * (Math.PI / 180.0);
var d2 = Latitude1 * (Math.PI / 180.0);
var num2 = Longitude1 * (Math.PI / 180.0) - num1;
var d3 = Math.Pow(Math.Sin((d2 - d1) / 2.0), 2.0) + Math.Cos(d1) * Math.Cos(d2) * Math.Pow(Math.Sin(num2 / 2.0), 2.0);
//var d3 = Math.Sqrt((Math.Pow(d1 - d2 , 2) + Math.Pow(num1 - num2, 2)));
Console.WriteLine(d3);
return 6376500.0 * (2.0 * Math.Atan2(Math.Sqrt(d3), Math.Sqrt(1.0 - d3)));
var d4 = Latitude2 * (Math.PI / 180.0);
var num4 = Longitude2 * (Math.PI / 180.0) - num1;
var d5 = Math.Pow(Math.Sin((d4 - d1) / 2.0), 2.0) + Math.Cos(d1) * Math.Cos(d4) * Math.Pow(Math.Sin(num4 / 2.0), 2.0);
//var d3 = Math.Sqrt((Math.Pow(d1 - d2 , 2) + Math.Pow(num1 - num2, 2)));
Console.WriteLine(d5);
return 6376500.0 * (2.0 * Math.Atan2(Math.Sqrt(d5), Math.Sqrt(1.0 - d5)));
and these is my code but it is print only one nearest place name i want all names from the list nearest to me and after that the calculated distance of that it has to ascending order but how....