How to check if a location is in MapPolygon in a Silverlight Bing Maps control?

I have a MapPolygon that covers a specific area in a Silverlight Bing Maps control, and I would like to know if a specific location is in that MapPolygon.

I tried the following code, which does not return the result that I want, because it checks whether the checked location is one of the MapPolygon vertices and does not check if this location 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 if two MapPolygons intersect with each other?

+4
source share
2 answers

Polygon. LOCATIONS - This is a list of points defining a polygon.

You need to make a way to find if your point is inside the polygon.

Use something like this (not tested 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.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 name it as follows:

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

Of course, both of these things are pretty trivial, take a look at the next article. http://msdn.microsoft.com/en-us/library/cc451895.aspx It provides good methods for finding the Bounding Box, Radius, and Polygon Search. The pointInPolygon method deserves special attention.

+3
source

Source: https://habr.com/ru/post/1315273/


All Articles