4

Silverlight Bing Maps コントロールの特定の領域をカバーする MapPolygon があり、特定の場所がこの MapPolygon 内にあるかどうかを知りたいです。

テストされた場所が MapPolygon の頂点の 1 つであるかどうかのみを確認し、この場所がこの MapPolygon 内に含まれているかどうかを確認しないため、必要な結果を返さない次のコードを試しました。

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

2 つの MapPolygons が互いに交差しているかどうかを判断することもできますか?

4

2 に答える 2

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 
}
于 2010-07-20T12:11:11.980 に答える
3

確かに、これらのことはどちらもかなり些細なことです。次の記事を見てください。http://msdn.microsoft.com/en-us/library/cc451895.aspxバウンディング ボックス、半径、およびポリゴン検索に適した方法を提供します。特に、pointInPolygon メソッドに注意してください。

于 2010-07-19T13:29:54.547 に答える