簡體   English   中英

使用linq查找具有給定緯度和經度的最近數據

[英]Using linq to find out the nearest data with the given latitude and longitude

我有一個數據表,其中存儲了按緯度和經度存儲的數據。
在此處輸入圖片說明

我需要使用LINQ查詢來過濾此datable,並從給定的緯度和經度中找到最近的位置。

例如:28.9873-緯度,80.1652-經度

嘗試這個:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM