Simple label on a leaflet (geojson) polygon

后端 未结 2 1836
悲哀的现实
悲哀的现实 2020-12-24 06:15

I am attempting what I imagine to be a fairly common use-case with a leaflet multipolygon object.

I create the MultiPolygon using geojson:

var layer          


        
2条回答
  •  天涯浪人
    2020-12-24 06:41

    You can use the onEachFeature option of L.geoJson to create a new L.divIcon for each polygon.

    L.geoJson(geoJsonData, {
      onEachFeature: function(feature, layer) {
        var label = L.marker(layer.getBounds().getCenter(), {
          icon: L.divIcon({
            className: 'label',
            html: feature.properties.NAME,
            iconSize: [100, 40]
          })
        }).addTo(map);
      }
    );
    

提交回复
热议问题