/// <summary> /// Find single stop nearest to multiple location /// </summary> /// <param name="location"></param> /// <param name="radius"></param> public static Stop CloestToMultipleLocation(List<double[]> locations, decimal radius = 500) { List<Stop> list = new List<Stop>(); using (ATDataContext context = new ATDataContext()) { list = context.Stops.ToList(); } GeoCoordinate endpoint = new GeoCoordinate(); GeoCoordinate startpoint = new GeoCoordinate(); double distance = 0; double new_distance = 0; Stop result = new Stop(); foreach (Stop s in list) { new_distance = 0; startpoint = new GeoCoordinate(s.Latitude.GetValueOrDefault(), s.Longitude.GetValueOrDefault()); foreach(var location in locations) { endpoint = new GeoCoordinate(location[0], location[1]); new_distance += startpoint.GetDistanceTo(endpoint); } if (list.IndexOf(s) == 0 || new_distance < distance) { distance = new_distance; result = s; } } return result; }
/// <summary> /// Find single stop nearest to single location /// </summary> /// <param name="location"></param> /// <param name="radius"></param> public static Stop CloestToSingleLocation(double[] location) { List<Stop> list = new List<Stop>(); using (ATDataContext context = new ATDataContext()) { list = context.Stops.ToList(); } GeoCoordinate endpoint = new GeoCoordinate(location[0], location[1]); GeoCoordinate startpoint = new GeoCoordinate(); double distance = 0; double new_distance = 0; Stop result = new Stop(); foreach (Stop s in list) { startpoint = new GeoCoordinate(s.Latitude.GetValueOrDefault(), s.Longitude.GetValueOrDefault()); new_distance = startpoint.GetDistanceTo(endpoint); if (list.IndexOf(s) == 0 || new_distance < distance) { distance = new_distance; result = s; } } return result; }