I am attempting to create my first leaflet map. I am using OpenStreetMap tiles and adding geojson features as overlay layers. I have been following tutorials with much success, until I started trying to add labels. I would simply like to add static labels to several features, and be able to toggle them on/off with the layer control. I have been using leaflet.label. Is this possible?
I have tried the following to give me labels with mixed results:
function onEachFeature(feature, layer) { if (feature.properties && feature.properties.name) { //layer.bindLabel(feature.properties.name, { 'noHide': true}); //var label = new L.Label(); //label.setContent(feature.properties.name); //label.setLatLng(layer.getBounds().getCenter()); //map.showLabel(label); } }Here is my layer control:
var overlayMaps = { "Layer 1": L.geoJson(layer1geojson, {onEachFeature: onEachFeature}).addTo(map), "Layer 2": L.geoJson(layer2geojson, {onEachFeature: onEachFeature}).addTo(map), "Buildings": L.geoJson(buildingsGeojson, {onEachFeature: onEachFeature}).addTo(map) };var legend = L.control.layers(baseMaps, overlayMaps, {collapsed:false});legend.addTo(map);
أكثر...
I have tried the following to give me labels with mixed results:
function onEachFeature(feature, layer) { if (feature.properties && feature.properties.name) { //layer.bindLabel(feature.properties.name, { 'noHide': true}); //var label = new L.Label(); //label.setContent(feature.properties.name); //label.setLatLng(layer.getBounds().getCenter()); //map.showLabel(label); } }Here is my layer control:
var overlayMaps = { "Layer 1": L.geoJson(layer1geojson, {onEachFeature: onEachFeature}).addTo(map), "Layer 2": L.geoJson(layer2geojson, {onEachFeature: onEachFeature}).addTo(map), "Buildings": L.geoJson(buildingsGeojson, {onEachFeature: onEachFeature}).addTo(map) };var legend = L.control.layers(baseMaps, overlayMaps, {collapsed:false});legend.addTo(map);
أكثر...