[GIS] Inner bounds of polygon in Leaflet

javascriptleafletmapbox

How can I find the inner bounds of a polygon in Leaflet? I a want to plot points randomly in polygon but when I say

Layer.getBounds() 

I get the bounds and I can still plot the markers approximately with in the saure i draw except most of them fall far out the intended polygons. This is because, getBounds() gives me out bounds.

Does any one have an idea how I can get inner bounds of a polygon?

Best Answer

Mapbox has a pointinpolygon you could use. https://github.com/mapbox/leaflet-pip Layer must be: L.geoJSON layer

Then use something like :

var sw = polygon.getBounds().getSouthWest();
var ne = polygon.getBounds().getNorthEast();


for (var i = 0; i < 25; i++) {
  var randomLat = Math.random() * (ne.lat() - sw.lat()) +sw.lat();
  var randomLng = Math.random() * (ne.lng() - sw.lng()) +sw.lng();
  var p = new L.latlng(randomLat, randomLng);
  if (leafletPip.pointInLayer(p, polygon)) {
    var mark = new L.marker(p).addTo(map);
    break;
  }
}