JSFiddle - React, Tailwind, and code Playground

by Stefano

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/Turf.js/2.0.2/turf.min.js"></script>
<div id="p1"></div>
<div id="p2"></div>

JavaScript

function isInside(testPolygon, targetPolygon) {
    var points = turf.explode(testPolygon);

    for (var i=0; i < points.features.length; i++) {
        if (!turf.inside(points.features[i], targetPolygon)) {
            return false;
        }
    }
    return true;
}

var ptIn = {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [ 11.436767578124998, 43.9058083561574 ]
      }
    };
var ptOut = {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [ 12.886962890625, 43.32517767999296 ]
      }
    };
var poly = {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [10.92041015625, 44.10336537791152 ],
            [ 10.8984375, 42.81152174509788 ],
            [ 12.513427734375, 42.83569550641454 ],
            [ 12.469482421875, 44.07969327425713 ],
            [ 10.92041015625, 44.10336537791152 ]
          ]
        ]
      }
    };

document.getElementById("p1").innerHTML = isInside(ptIn, poly);
document.getElementById("p2").innerHTML = isInside(ptOut, poly);


//=isInside2