Determine if point intersects 35km radius around another point? Possible in Linq?
Assume I h开发者_运维技巧ave a point at the following location:
Latitude: 47°36′N Longitude: 122°19′W
Around the above point, I draw a 35Km radius. I have another point now or several and I want to see if they fall within the 35Km radius? How can I do this? Is it possible with Linq given the coordinates (lat, long) of both points?
Sure. Assume you have a function that computes the Haversine distance between two Positions (consisting of a latitude and longitude coordinate). If you don't you can find one here. Then simply use the function as the selector in a Where clause. If using LINQ to SQL, you'll need to materialize them to your Position objects so that the you can use the Haversine function on them as LINQ to objects; there isn't a translation to SQL, though you could probably create a table-valued function that does the same thing if you really don't want to return all the points first.
var origin = new Position( 47.6, 122.3 );
var close = positions.Where( p => Haversine.Distance( origin, p, DistanceType.Km ) <= 35 );
精彩评论