// radius is the half length of the bounding box you want in kilometers. public static BoundingBox GetBoundingBox(MapPoint point, double radiusInKm) { // Bounding box surrounding the point at given coordinates, // assuming local approximation of Earth surface as a sphere // of radius given by WGS84 var lat = Deg2rad(point.Latitude); var lon = Deg2rad(point.Longitude); var halfSide = 1000 * radiusInKm; // Radius of Earth at given latitude var radius = WGS84EarthRadius(lat); // Radius of the parallel at given latitude var pradius = radius * Math.Cos(lat); var latMin = lat - halfSide / radius; var latMax = lat + halfSide / radius; var lonMin = lon - halfSide / pradius; var lonMax = lon + halfSide / pradius; return new BoundingBox { MinPoint = new MapPoint { Latitude = Rad2deg(latMin), Longitude = Rad2deg(lonMin) }, MaxPoint = new MapPoint { Latitude = Rad2deg(latMax), Longitude = Rad2deg(lonMax) } }; }
private const double WGS84_b = 6356752.3; // Minor semiaxis [m] public static double HaversineDistance(MapPoint pos1, MapPoint pos2, DistanceUnit unit) { double R = (unit == DistanceUnit.Miles) ? 3960 : 6371; var lat = Deg2rad(pos2.Latitude - pos1.Latitude); var lng = Deg2rad(pos2.Longitude - pos1.Longitude); var h1 = Math.Sin(lat / 2) * Math.Sin(lat / 2) + Math.Cos(Deg2rad(pos1.Latitude)) * Math.Cos(Deg2rad(pos2.Latitude)) * Math.Sin(lng / 2) * Math.Sin(lng / 2); var h2 = 2 * Math.Asin(Math.Min(1, Math.Sqrt(h1))); return R * h2; }