public static double DistanceTo(this Coordinates baseCoordinates, Coordinates targetCoordinates, UnitOfLength unitOfLength) { var baseRad = Math.PI * baseCoordinates.Latitude / 180; var targetRad = Math.PI * targetCoordinates.Latitude / 180; var theta = baseCoordinates.Longitude - targetCoordinates.Longitude; var thetaRad = Math.PI * theta / 180; double dist = Math.Sin(baseRad) * Math.Sin(targetRad) + Math.Cos(baseRad) * Math.Cos(targetRad) * Math.Cos(thetaRad); dist = Math.Acos(dist); dist = dist * 180 / Math.PI; dist = dist * 60 * 1.1515; return(unitOfLength.ConvertFromMiles(dist)); }
public SortedDictionary <double, Entry> FilterByLocation(Coordinates center, double distance = 5, UnitOfLength length = null) { if (length == null) { length = UnitOfLength.Kilometers; } SortedDictionary <double, Entry> result = new SortedDictionary <double, Entry>(); foreach (var entry in RepeaterBookData.Entries) { var d = entry.Coordinates.DistanceTo(center, length); if (d <= distance) { while (result.ContainsKey(d)) { //add an arbitrarily tiny amount, just to keep our keys unique d = d + 0.0000000001; } result.Add(d, entry); } } return(result); }