0

I have a Location Table and save Lat,Lon of loctions in it.

enter image description here

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=>?)
ar.gorgin
  • 4,765
  • 12
  • 61
  • 100
  • Does this answer your question? [Calculate distance between two latitude-longitude points? (Haversine formula)](https://stackoverflow.com/questions/27928/calculate-distance-between-two-latitude-longitude-points-haversine-formula) – derpirscher May 30 '21 at 09:03
  • Thanks, but i wanto to use in linq – ar.gorgin May 30 '21 at 10:34

1 Answers1

0

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);
derpirscher
  • 14,418
  • 3
  • 18
  • 35