Union function using Turf.js and Leaflet Geodesy
HTML
<script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css">
<script src="https://api.mapbox.com/mapbox.js/plugins/leaflet-geodesy/v0.1.0/leaflet-geodesy.js"></script>
<script src="https://api.mapbox.com/mapbox.js/plugins/turf/v2.0.2/turf.min.js"></script>
<div id="map"></div>
CSS
#map {
height: 500px;
}
JavaScript
var parentCoordinates=[
[
32.05898221582174,
-28.31004731142091
],
[
32.05898221582174,
-28.308044824292978
],
[
32.06134255975485,
-28.308044824292978
],
[
32.06134255975485,
-28.31004731142091
],
[
32.05898221582174,
-28.31004731142091
]
];
var childCoordinates=[
[
32.059904895722866,
-28.30970726909422
],
[
32.059904895722866,
-28.308743809931784
],
[
32.06089194864035,
-28.308743809931784
],
[
32.06089194864035,
-28.30970726909422
],
[
32.059904895722866,
-28.30970726909422
]
];
var parentPolygon = turf.polygon([parentCoordinates]);
var inside = true;
childCoordinates.forEach(function(coordinates) {
point = turf.point(coordinates);
if (!turf.inside(point, parentPolygon)){
alert("Oh no! "+ coordinates + " isn't in polygon");
inside = false;
}
});
alert("Child polygon inside parent polygon ? " + inside);