简体   繁体   English

多边形中的传单可点击网格

[英]Leaflet clickable grid in polygon

I'm using leaflet.js to show reforestation efforts.我正在使用 Leaflet.js 来展示重新造林的努力。

在此处输入图片说明

Is there any possibility to create a grid-pattern where every square of the pattern can be linked to a click-event?有没有可能创建一个网格模式,其中模式的每个方块都可以链接到一个点击事件?
Something similar like this .同样的事情也喜欢这样
It would need small squares that would together form a similar polygon as shown above.如上所示,它需要小方块共同形成一个类似的多边形。

I tried Leaflet-pattern, but the squares resize on zoom and there is no option to add an event to the pattern shapes.我尝试了 Leaflet-pattern,但正方形会在缩放时调整大小,并且无法向图案形状添加事件。

Could I use the leaflet rectangle for this?我可以为此使用传单矩形吗? How would I find the correct latitude and longitudes for each square?我如何找到每个正方形的正确纬度和经度?

You probably want to create a square grid , then calculate the intersection between each grid cell with each polygon.您可能想要创建一个方形网格,然后计算每个网格单元与每个多边形之间的交集

It's up to you to decide the details of the square grid, and whether you want the same grid for all polygons, or a different grid for each polygon.由您决定方形网格的详细信息,以及是要为所有多边形使用相同的网格,还是为每个多边形使用不同的网格。

Thanks to Ivan Sanchez I found the solution.感谢Ivan Sanchez,我找到了解决方案。 For anyone looking for it see this JSFiddle .对于任何寻找它的人,请参阅此JSFiddle

''' '''

    // initialize the map on the "map" div with a given center and zoom
    var map = L.map(
      'map', {
        layers: [map]
      }
    ).setView([-5.0, 19.40], 11);

    //HexGrid
    var bbox = [19.35, -5, 19.5, -5.15];
    var cellSide = 1;
    var options = {
      units: 'kilometers'
    };
    var hexgrid = turf.hexGrid(bbox, cellSide, options);

    //Polygram that will contain the hexagons
    let poly1 = turf.polygon([
      [
        [19.4, -5],
        [19.5, -5.1],
        [19.4, -5.1],
        [19.4, -5]
      ]
    ]);

    _.each(hexgrid.features, function(hex) {
      var intersection = turf.intersect(poly1, hex.geometry);
      if (intersection) {
        hex.geometry = intersection.geometry;
      } else {
        hex.geometry = {
          type: "Polygon",
          coordinates: []
        }
      }
    })


    L.geoJSON(hexgrid, {
      style: function(feature) {
        return {
          weight: 1
        };
      }
    }).addTo(map);
    L.geoJSON(poly1).addTo(map);

''' '''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM