开发者

How can I check whether a location is within a MapPolygon in the Silverlight Bing Maps control?

I have a MapPolygon which covers a certain area on the Silverlight Bing Maps control, and I would like to know if a particular Location is located within this MapPolygon.

I have tried the following code which doesen't return the result I want because it only checks if the tested location is one of the vertices of t开发者_StackOverflow中文版he MapPolygon, and doesn't check if this Location is contained within this MapPolygon.

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

Is it also possible to determine if two MapPolygons intersect one another?


The polygon.Locations is a list of points defining the polygon.

You have to make a method to find if your point is inside the polygon.

Use something like this (not tested if compiles):

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;
}

And call it like this:

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


Sure both of these things are fairly trivial, take a look at the following article. http://msdn.microsoft.com/en-us/library/cc451895.aspx It gives good methods for Bounding Box, Radius, and Polygon Search. Particularity take note of the pointInPolygon method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜