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


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?

  • There isn't really a concept of an "inner bounds". Bounds are the maximum and minimum x and y (or lat and lon, x being lon) the polygon touches. Oct 20, 2014 at 20:43
  • Okay. So i can't possibly draw a square inside the polygon. Does this mean what i have is as close as am going to get to plotting random markers with in a district? Oct 20, 2014 at 20:47
  • 1
    You could use getLatLngs to get the vertices, generate points within the bounds, and use the even-odd rule en.wikipedia.org/wiki/Even%E2%80%93odd_rule to determine whether your points are inside the polygon. It would take a bit of scripting. Oct 20, 2014 at 20:48
  • 1
    following up on what @RussellatISC said, you could use something like Leaflet point in polygon in conjunction with getBounds(), to find points that only fall within the polygon.
    – toms
    Oct 20, 2014 at 21:23
  • Thanks @Russell and toms ... I should now be able to make my point more accurate. I will post my findings when I Finnaly get it plot accurately. Oct 20, 2014 at 21:29

2 Answers 2


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);
  • 1
    This can work, but the problem is that the random point is generated with a square box vs the actual polygon, so if the point is inside the bounding box, but outside the polygon itself, you'll need to call your function (in the example above, a loop) again until a match (pip) is found
    – mheavers
    Sep 15, 2015 at 18:01

I have done with below code:

function getPolygonBounds() {
    var polygons = []
    map.eachLayer(function (layer) {
        if (layer instanceof L.Polygon && !(layer instanceof L.Rectangle)) {
            polygons.push(layer.getLatLngs()) //Returns an array of arrays of geographical points in each polygon.
            polygons.push(layer.getBounds()) //Returns a GeoJSON representation of the polygon (GeoJSON Polygon Feature).
    return polygons

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.