Simple label on a leaflet (geojson) polygon

后端 未结 2 1844
悲哀的现实
悲哀的现实 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: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/

提交回复
热议问题