I have a Location Table and save Lat,Lon of loctions in it.
I want to find Locations in radius of special location
How to find it?
var lon = 52.12457;
var lat = 58.9542154;
var locations=db.Locations.Where(m=>?)
I have a Location Table and save Lat,Lon of loctions in it.
I want to find Locations in radius of special location
How to find it?
var lon = 52.12457;
var lat = 58.9542154;
var locations=db.Locations.Where(m=>?)
Translating the JS function from this answer from my above comment to C# gives you the following
public static double GetDistance(double lat1, double lon1, double lat2, double lon2) {
var R = 6371; // Radius of the earth in km
var dLat = Deg2Rad(lat2-lat1); // deg2rad below
var dLon = Deg2Rad(lon2-lon1);
var a = Math.Sin(dLat/2) * Math.Sin(dLat/2) +
Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) *
Math.Sin(dLon/2) * Math.Sin(dLon/2);
var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1-a));
var d = R * c; // Distance in km
return d * 1000; //distance in m
}
public static double Deg2Rad(double deg) {
return deg * (Math.PI/180);
}
which you then can easily call in your Where
clause
var lon = 52.12457;
var lat = 58.9542154;
var locations = db.Locations.Where(m=> GetDistance(lat, lon, m.lat, m.lon) < 15);