Leaflet.PointInPolygon is a Leaflet plugin/extension that provides point-in-polygon functions
based on Dan Sunday's C++ winding number implementation. Functions contains()
, getWindingNumber()
, and isLeft()
extend
Leaflet classes L.Polygon
, L.Polyline
, and L.LatLng
.
The winding number method has an advantage to the crossing number method since
"the winding number accurately determines if a point is inside a nonsimple closed polygon"
(See Inclusion of a Point in Polygon by Dan Sunday).
Unlike the C++ implementation,
points on the lines and vertices are considered as being included in the polygon. As such, the getWindingNumber()
function will
work for a polyline. The inclusiveness of points on the the polygon lines (and polylines) has a natural interpretation for
locations on maps. A future version of Leaflet.PointInPolygon will support the ability to be inclusive or exclusive.
- Leaflet: This has been tested on a variant of Leaflet version 0.7.7, version Leaflet 1.0.3 (from GitHub), and Leaflet.Geodesic dated April 26, 2013.
Demo on JSFiddle: Leaflet.PointInPolygon Demo. And if you want to fork a copy of the fiddle, click here. Click on the map or the markers and a pop-up will show indicating if the point you clicked on, or the marker, is in the polygon.
![Leaflet.PointInPolygon Demo screen shot] (https://raw.githubusercontent.com/hayeswise/Leaflet.PointInPolygon/master/DemoScreenShot.PNG) (Clickable image)
Leaflet.PointInPolygon is plugin was Originally developed for the Ingress Intel Total Conversion(IITC). If you have a Ingress account, install the Ingress Intel Total Conversion desktop and the install the Portals-in-Polygons plugin.
Use the script tag and pull from the CDN.
RawGit Production CDN:
<script src="https://cdn.rawgit.com/hayeswise/Leaflet.PointInPolygon/v1.0.0/wise-leaflet-pip.js"></script>
RawGit Development CDN
<script src="https://rawgit.com/hayeswise/Leaflet.PointInPolygon/master/wise-leaflet-pip.js"></script>
If you are using Greasemonkey or Tampermonkey scripts, as done with IITC plugins, use one of the following require metadata block keys and values:
-
RawGit Production CDN:
@require https://cdn.rawgit.com/hayeswise/Leaflet.PointInPolygon/v1.0.0/wise-leaflet-pip.js -
RawGit Development CDN:
@require https://rawgit.com/hayeswise/Leaflet.PointInPolygon/master/wise-leaflet-pip.js
See API.
var polygon = L.polygon([
[51.51, -0.08],
[51.503, -0.06],
[51.51, -0.047]
]).addTo(mymap);
var m1 = L.marker([51.515, -0.07]); // Outside and north of polygon
var m2 = L.marker([51.506, -0.06]); // In polygon, not on border
var m2 = L.marker([51.505, -0.074]); // Inside polygon boundary box but outside of polygon.
var m2 = L.marker([51.51, -0.067]); // On polygon border.
console.log(polygon.contains(m1.getLatLng()));
// ==> false
console.log(polygon.contains(m2.getLatLng()));
// ==> true
console.log(polygon.contains(m3.getLatLng()));
// ==> false
console.log(polygon.contains(m4.getLatLng()));
// ==> true
For a more comprehensive example using the above code, see Leaflet.PointInPolygon Demo. Click on the Javascript tab to see the code.
CDN services provided by RawGit. This is useful for referencing the code distribution in JSFiddle. Just note that the CDN updates lag behind the GitHub updates. Production updates are behind Development updates. The Production CDN has no limits or throttling (but since this is a free service, there's no uptime or support guarantees).
- Via RawGit Production.
- Via RawGit Development.
Thanks to:
- Inclusion of a Point in Polygon by Dan Sunday
- JSHint
- CondorTheGreat for the first demo at JSFiddle (https://jsfiddle.net/CondorTheGreat/hhqqmbqc/)
- RawGit and StackPath
- Leaflet