There are 2 methods to finding the location by an unexact coordinate. The first solution is to use a coordinate variance, the second is to use distance. Each have their own benefits so choose wisely.
Solution 1:
Since the coordinates are not exactly matching we must come up with an allowed variance. For this example i will use 0.001 variance for Lat and Long.
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
public class Program {
public class Location {
public string Country { get; set; }
public string City { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public Location(string Country_, string City_, double Latitude_, double Longitude_){
this.Country = Country_;
this.City = City_;
this.Latitude = Latitude_;
this.Longitude = Longitude_;
}
}
public static void Main() {
List<Location> locations = new List<Location>();
locations.Add(new Location("USA", "Daytona Beach", 29.2108, 81.0228));
locations.Add(new Location("USA", "Miami", 25.7617,80.1918));
locations.Add(new Location("USA", "Jacksonville", 30.3322,81.6557));
double variance = 0.001;
double searchLat = 29.21; //unexact lat of daytona beach
Console.WriteLine(
locations.Where(w => w.Latitude >= searchLat - variance && w.Latitude <= searchLat + variance)
.Select(s => s.City)
.DefaultIfEmpty("Unknown")
.First()
.ToString()
);
}
}
Solution 2: I don't really recommend this because of how process heavy this can be but just incase distance is actually important..
You will need to iterate over each of the location coordinates with your search coordinate and select the smallest distance.
Coordinate method ripped from here: Calculating Distance between two Latitude and Longitude GeoCoordinates
using System;
using System.Collections.Generic;
using System.Linq;
public class Program {
public class Location {
public string Country { get; set; }
public string City { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public Location(string Country_, string City_, double Latitude_, double Longitude_) {
this.Country = Country_;
this.City = City_;
this.Latitude = Latitude_;
this.Longitude = Longitude_;
}
}
static void Main(string[] args) {
List<Location> locations = new List<Location>();
locations.Add(new Location("USA", "Daytona Beach", 29.2108, 81.0228));
locations.Add(new Location("USA", "Miami", 25.7617, 80.1918));
locations.Add(new Location("USA", "Jacksonville", 30.3322, 81.6557));
double searchLat = 30;
double searchLong = 81;
Dictionary<double, List<Location>> distances = new Dictionary<double, List<Location>>();
locations.ForEach(location => {
double distance = Program.GetDistance(location.Latitude, location.Longitude, searchLat, searchLong);
if(distances.ContainsKey(distance)){
distances[distance].Add(location);
}else{
distances.Add(distance, new List<Location>() { location });
}
});
double closestDistanceFromSearch = distances.Keys.OrderBy(k => k).First();
Console.WriteLine(
distances[distances.Keys.OrderBy(k => k).First()].First().City
);
}
public static double GetDistance(double latitude, double longitude, double otherLatitude, double otherLongitude) {
var d1 = latitude * (Math.PI / 180.0);
var num1 = longitude * (Math.PI / 180.0);
var d2 = otherLatitude * (Math.PI / 180.0);
var num2 = otherLongitude * (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);
return 6376500.0 * (2.0 * Math.Atan2(Math.Sqrt(d3), Math.Sqrt(1.0 - d3)));
}
}