2010-08-15 59 views
1

我有一些代碼的地址彼此相距5英里。這是通過循環地址列表並計算第一個地址和其餘地址之間的站點距離來完成的。然後將列表中的第一個地址以及任何匹配的地址放入另一個列表中。計算中心的批次的位置

一旦我有地址批處理,我想要abe來計算批處理的中心。目前我使用的是第一個地址,但是我想要更好的準確性。因爲我有經緯度的可以使用數學函數嗎?

馬克

回答

3

該中心將是所有地區的平均:

var center = new Location 
      { 
       Latitude = list.Average(loc => loc.Latitude), 
       Longitude = list.Average(loc => loc.Longitude) 
      }; 
+0

這不會給你的中心。如果幾個地址彼此靠近(例如在一個城市),另一個在該地區的另一邊,那麼您的位置會偏離該羣體而不是真正的中心。 – 2015-06-25 20:33:02

+0

@JamesCurran,是的,這是正確的。參見[維基百科的質心](https://en.wikipedia.org/wiki/Centroid)。現在,你可能對「中心」有不同的定義;我的代碼計算幾何中心。 – 2015-06-25 21:32:13

2

「中心」將是(我相信)最高&最低latitute的平均值,最高和最低longitute。

var hilat = list.Max(loc=>loc.Latitute); 
    var lolat = list.Min(loc=>loc.Latitute); 
    var hilng = list.Max(loc=>loc.Longitute); 
    var lolng = list.Min(loc=>loc.Longitute); 

    var center = new Location() { Latitute = (hilat-lolat)/2 + lolat, 
           Logitute = (hilng-lolng)/2 + lolng};