Simple label on a leaflet (geojson) polygon

后端 未结 2 1834
悲哀的现实
悲哀的现实 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);
      }
    );
    
    0 讨论(0)
  • 2020-12-24 06:54

    The leaflet label plugin also allows static labels, see the demo. The only reason the polyline label is not static is that it moves around as you move along the polyline.

    You can probably do better than this, by overriding bindLabel() for Polygons but this is a simple way to add a static label to the center of a polygon:

    label = new L.Label()
    label.setContent("static label")
    label.setLatLng(polygon.getBounds().getCenter())
    map.showLabel(label);
    

    http://jsfiddle.net/CrqkR/6/

    0 讨论(0)
提交回复
热议问题