Check if the point is in the polygon (maps)

I am trying to check if a point is in a polygon.
At the moment, I tried using this function

pointInPolygon:function (point,polygon){ var i; var j=polygon.length-1; var inPoly=false; var lon = point.longitude; var lat = point.latitude; for (i=0; i<polygon.length; i++) { if (polygon[i][0]<lon && polygon[j][0]>=lon|| polygon[j][0]<lon && polygon[i][0]>=lon){ if (polygon[i][0]+(lon-polygon[i][0])/(polygon[j][0]-polygon[i][0])*(polygon[j][1]-polygon[i][1])<lat){ inPoly=!inPoly; } } j=i; } return inPoly; } 

... this function works on a simple polygon ( http://jsfiddle.net/zTmr7/3/ ), but it will not work for me ... here is the sample data from the polygon:

 polygon: Array[14] Array[2] 0: "-120.190625" 1: "29.6614549946937" Array[2] 0: "-116.87275390625" 1: "32.6320990313992" Array[2] 0: "-116.60908203125" 1: "34.0363970332393" Array[2] 0: "-120.89375" 1: "41.9203747676428" Array[2] 0: "-114.74140625" 1: "45.784484644005" Array[2] 0: "-115.971875" 1: "48.6489780115889" Array[2] 0: "-132.758984375" 1: "59.9891712248332" Array[2] 0: "-162.5099609375" 1: "68.919753529737" Array[2] 0: "-168.6623046875" 1: "68.9828872543805" Array[2] 0: "-168.4865234375" 1: "64.2551601036027" Array[2] 0: "-179.874356794357" 1: "51.0915874974707" Array[2] 0: "-179.999916362762" 1: "13.1823178795562" Array[2] 0: "-143.8771484375" 1: "19.9962034117847" Array[2] 0: "-120.190625" 1: "29.6614549946937" 

Maybe you can help ... thanks in advance

PS. The solution should be especially for Bing cards or a universal solution ...

+4
source share
4 answers

The Google Maps API does not yet provide a method for checking points in polygons. After doing some research, I came across a Ray-casting algorithm that will determine if the XY coordinate is inside the constructed shape. This will lead to latitude and longitude. The following extends google.maps.polygon.prototype to use this algorithm. Just include this code at a point in the code after loading google.maps:

 google.maps.Polygon.prototype.Contains = function(point) { var crossings = 0, path = this.getPath(); // for each edge for (var i=0; i < path.getLength(); i++) { var a = path.getAt(i), j = i + 1; if (j >= path.getLength()) { j = 0; } var b = path.getAt(j); if (rayCrossesSegment(point, a, b)) { crossings++; } } // odd number of crossings? return (crossings % 2 == 1); function rayCrossesSegment(point, a, b) { var px = point.lng(), py = point.lat(), ax = a.lng(), ay = a.lat(), bx = b.lng(), by = b.lat(); if (ay > by) { ax = b.lng(); ay = b.lat(); bx = a.lng(); by = a.lat(); } // alter longitude to cater for 180 degree crossings if (px < 0) { px += 360 }; if (ax < 0) { ax += 360 }; if (bx < 0) { bx += 360 }; if (py == ay || py == by) py += 0.00000001; if ((py > by || py < ay) || (px > Math.max(ax, bx))) return false; if (px < Math.min(ax, bx)) return true; var red = (ax != bx) ? ((by - ay) / (bx - ax)) : Infinity; var blue = (ax != px) ? ((py - ay) / (px - ax)) : Infinity; return (blue >= red); } }; 

Here we have expanded the functionality of google.maps.Polygon by specifying a function called โ€œContainsโ€, which can be used to determine whether the latitude longitude is in the function parameter inside the polygon or not. Here we use the Ray-casting algorithm and have developed a function using the same. After completing this large amount of exercise, we can check the point as follows:

var point = new google.maps.LatLng (52.05249047600099, -0.6097412109375); var polygon = new google.maps.Polygon ({path: [INSERT_PATH_ARRAY_HERE]}); if (polygon.Contains (point)) {// point inside the polygon}

For a complete code and demo, go to: http://counsellingbyabhi.blogspot.in/2013/01/google-map-check-whether-point-latlong.html

+3
source

The first if looks good - you check to see if the longitude of the point is within the longitude of the polygon segment.

The second if should interpolate the interception of the segment with the exact longitude of the point and determine whether this interception is above or below the point. I do not think that this is what he does, because of a simple typo.

 if (polygon[i][1]+(lon-polygon[i][0])/(polygon[j][0]-polygon[i][0])*(polygon[j][1]-polygon[i][1])<lat){ ^ 

You should also include a separate case where polygon[i][0]==polygon[j][0] so that you don't get a zero delimited error.

+1
source

You can use my clone of the libkml variant, which I reflected on github here: https://github.com/gumdal/libkml-pointinpolygon

With the help of the author of this open source, a module has been developed that will indicate whether a given point is inside the KML polygon or not. Make sure that you check the "libkml-git" branch and not the "leading" branch of git sources. The class that interests you is "pointinpolygon.cc". This is the C ++ source code that you can include in your project and create it with your project.

Change The solution to the problem with a point in the polygon does not depend on which map it is superimposed on.

0
source

true | false = google.maps.geometry.poly.containsLocation (googlePoint, googlePoly);

0
source

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


All Articles