Polygonal labels with sheet

Using leaflet 7.3 and polygonal data in GeoJSON, how can I add labels from the: NAME field?

Here is an example of current GeoJSON geological data. I would like to enable fixed labels at the center of the polygon, overlap OK.

var districts = L.geoJson(null, {
  style: function (feature) {
    return {
      color: "green",
      fill: true,
      opacity: 0.8
    };
  },


onEachFeature(feature, layer) {
    layer.on('mouseover', function () {
      this.setStyle({
        'fillColor': '#0000ff'
      });
    });
    layer.on('mouseout', function () {
      this.setStyle({
        'fillColor': '#ff0000'
      });
    });
    layer.on('click', function () {
    window.location = feature.properties.URL;
    });
}

});

$.getJSON("data/districts.geojson", function (data) {
  districts.addData(data);
});

      

+3


source to share


1 answer


In the onEachFeature callback you can get the center L.Polygon created by the GeoJSON layer and bind a label to it.

var polygonCenter = layer.getBounds().getCenter();

// e.g. using Leaflet.label plugin
L.marker(polygonCenter)
    .bindLabel(feature.properties['NAME'], { noHide: true })
    .addTo(map);

      



Here's an example: http://jsfiddle.net/FranceImage/ro54bqbz/ using Leaflet.label

+5


source







All Articles