How about if you extract the data from the database that fits in the square around your circle of desired distance, and then fine tune on the client side to just the posts in the circle?
Given your initial data
var myLat = 25.05;
var myLon = -80.3;
var radiusInMile = 50;
You can compute the bounds of the square containing that circle
var minMilePerLat = 68.703;
var milePerLon = Math.Cos(myLat) * 69.172;
var minLat = myLat - radiusInMile / minMilePerLat;
var maxLat = myLat + radiusInMile / minMilePerLat;
var minLon = myLon - radiusInMile / milePerLon;
var maxLon = myLon + radiusInMile / milePerLon;
Then you can query the database for those posts inside the square, bring them to the client and keep just the ones in the circle
var data = _context.Posts
.Where(p => (minLat <= p.Lat && p.Lat <= maxLat) && (minLon <= p.Lng && p.Lng <= maxLon))
.AsEnumerable()
.Select(p => new { p, Dist = distanceInMiles(myLon, myLat, p.Lng, p.Lat) })
.Where(p => p.Dist <= radiusInMile);
Where the distanceInMiles
function is defined as
public double ToRadians(double degrees) => degrees * Math.PI / 180.0;
public double distanceInMiles(double lon1d, double lat1d, double lon2d, double lat2d) {
var lon1 = ToRadians(lon1d);
var lat1 = ToRadians(lat1d);
var lon2 = ToRadians(lon2d);
var lat2 = ToRadians(lat2d);
var deltaLon = lon2 - lon1;
var c = Math.Acos(Math.Sin(lat1) * Math.Sin(lat2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Cos(deltaLon));
var earthRadius = 3958.76;
var distInMiles = earthRadius * c;
return distInMiles;
}
I am using the spherical law of cosines to compute the distance, which I assume is enough for this problem. If you need more accuracy, you could upgrade the formula to something more complicated like haversine or Vincenty.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…