与Leaflet的多边形标签

Ber*_*n88 3 javascript jquery geojson leaflet

在GeoJSON中使用传单7.3和多边形数据,如何在字段中添加标签:NAME?

以下是当前GeoJSON多边形数据的示例.我想在多边形的中心启用固定标签,重叠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);
});
Run Code Online (Sandbox Code Playgroud)

YaF*_*red 5

onEachFeature回调中,您可以获取由GeoJSON图层创建的L.Polygon的中心并将标签绑定到它.

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

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

这是一个例子:http://jsfiddle.net/FranceImage/ro54bqbz/使用Leaflet.label