[英]Using linq to find out the nearest data with the given latitude and longitude
嘗試這個:
var minDistance = collection.Min( x => Math.Sqrt( Math.Pow( wantedLatitude - x.Latitude ) + Math.Pow( wantedLongitude - x.Longitude )));
var closestLocation = collection.First( x => Math.Sqrt( Math.Pow( wantedLatitude - x.Latitude ) + Math.Pow(wantedLongitude - x.Longitude )) == minDistance );
它經過兩次收集,因此我建議通過在第一次運行中找到Min元素來對其進行優化,您可以輕松地在SO上的其他問題中找到如何做到這一點,並將其與方法的距離封裝在一起,但是您明白了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.