2010-07-10 46 views
4

我有一個MapPolygon覆蓋在Silverlight的Bing地圖的控制, 一定的區域,我想知道如果一個特定的地點位於該MapPolygon內。如何檢查位置是否位於Silverlight Bing Maps控件的MapPolygon中?

我嘗試了下面的代碼,它不返回我想要的結果,因爲它只檢查被測試的位置是否是MapPolygon的頂點之一,並且不檢查該位置是否包含在此MapPolygon中。

polygon.Locations.Contains(new Location(this.Site.Latitude, this.Site.Longitude, this.Site.Altitude)); 

也可以確定兩個MapPolygons是否相互交叉?

+0

這聽起來像你想使用光線投射算法。 http://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm 對不起,我不能幫助不止於此。我不熟悉Bing地圖控件。 – 2010-07-19 01:56:40

回答

3

的polygon.Locations是定義多邊形點的列表。

你必須做出一個方法來找到,如果你的點是多邊形內。

使用這樣的事情(如果編譯未測試):

static bool PointInPolygon(LocationCollection polyPoints, Location point) 
{ 

    if (polyPoints.Length < 3) 
    { 
     return false; 
    } 

    bool inside = false; 
    Location p1, p2; 

    //iterate each side of the polygon 
    Location oldPoint = polyPoints[polyPoints.Count - 1]; 

    foreach(Location newPoint in polyPoints) 
    { 
     //order points so p1.lat <= p2.lat; 
     if (newPoint.Latitude > oldPoint.Latitude) 
     { 
      p1 = oldPoint; 
      p2 = newPoint; 
     } 
     else 
     { 
      p1 = newPoint; 
      p2 = oldPoint; 
     } 

     //test if the line is crossed and if so invert the inside flag. 
     if ((newPoint.Latitude < point.Latitude) == (point.Latitude <= oldPoint.Latitude) 
      && (point.Longitude - p1.Longitude) * (p2.Latitude - p1.Latitude) 
      < (p2.Longitude - p1.Longitude) * (point.Latitude - p1.Latitude)) 
     { 
      inside = !inside; 
     } 

     oldPoint = newPoint; 
    } 

    return inside; 
} 

,並調用它是這樣的:

if (PointInPolygon(polygon.Locations, new Location(this.Site.Latitude, this.Site.Longitude, this.Site.Altitude))) 
{ 
    //do something 
} 
+0

中提供的函數functionInPolygon(points,lat,lon)即可得到我想要的,非常感謝。 – bahith 2010-07-21 07:16:53

相關問題