How do I check if the location is mappolygon in the Silverlight Bing Maps control?

I have a MapPolygon, which covers a certain area on the Silverlight Bing Maps control,
I want to know whether a specific location is located in this MapPolygon.

I tried the following code, it will not return the result I want, because it only checks whether the test position is one of the vertices of the MapPolygon, and does not check whether this position is included in this MapPolygon.

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

Is it possible Determine if two MapPolygons cross each other?

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

You must create a method to find out if your point is inside the polygon.

Use something like this (do not test if compiled):

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.Longitude-p1.Longitude) * (p2.Latitude-p1.Latitude)
<(p2.Longitude-p1.Longitude) * (point.Latitude-p1.Latitude))
{
inside = !inside;
}

oldPoint = newPoint;
}

return inside;
}

and call it:

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

I have a MapPolygon, which covers a certain area on the Silverlight Bing Maps control,
I want to know whether a specific location is located in this MapPolygon.

I tried the following code and it does not return the result I want because it only checks whether the test position is one of the vertices of the MapPolygon, and does not check whether this position is contained in this MapPolygon.

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

Is it also possible to determine whether two MapPolygons cross each other?

polygon.Locations is a list of points that define polygons.

You must create a method to find whether your points are in Within the polygon.

Use something like this (do not test if compiled):

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.Longitude-p1.Longitude) * (p2.Latitude-p1.Latitude)
<(p2.Longitude-p1.Longitude) * (point.Latitude -p1.Latitude))
{
inside = !inside;
}

oldPoint = newPoint;
}

return inside;
}

and call it:

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

Leave a Comment

Your email address will not be published.