Ale*_*lex 3 javascript jquery jquery-plugins geojson dc.leaflet.js
我正在使用带有 Leaflet.js 的 geoJson 层,在此处显示国家/地区。
我正在添加以下国家/地区标签:
L.marker(layer.getBounds().getCenter(), {
icon: L.divIcon({
className: 'countryLabel',
html: feature.properties.name,
iconSize: [0, 0]
})
}).addTo(map);
Run Code Online (Sandbox Code Playgroud)
问题是,这种应用的标记阻碍了每个国家/地区的鼠标悬停,导致鼠标悬停颜色变化和可点击区域出现问题。
传单 1.0.3 中是否有更好的解决方案来提供不会阻碍国家/地区可点击区域的标签?
我已经尝试过使用 Leaflet.Label 扩展的代码,如下所示:
var label = new L.Label();
label.setContent(feature.properties.name);
label.setLatLng(center);
map.showLabel(label);
Run Code Online (Sandbox Code Playgroud)
或者
L.marker(center)
.bindLabel('test', { noHide: true })
.addTo(map);
Run Code Online (Sandbox Code Playgroud)
但是这些会导致错误;我知道这个插件的功能在 v1 之后被并入 Leaflet.js 本身。
这确实有效,但我宁愿使用简单的标签而不是工具提示:
var marker = new L.marker(center, { opacity: 0.00 }); //opacity may be set to zero
marker.bindTooltip(feature.properties.name, { permanent: true, className: "my-label", offset: [0, 0] });
marker.addTo(map);
Run Code Online (Sandbox Code Playgroud)
欢迎任何想法。
我不明白你为什么要通过标记标记来完成。
您可以将工具提示直接绑定到要素。在您的函数onEachFeature中var label...,您可以执行以下操作:
layer.bindTooltip(
feature.properties.name,
{
permanent:true,
direction:'center',
className: 'countryLabel'
}
);
Run Code Online (Sandbox Code Playgroud)
使用这个 css:
.countryLabel{
background: rgba(255, 255, 255, 0);
border:0;
border-radius:0px;
box-shadow: 0 0px 0px;
}
Run Code Online (Sandbox Code Playgroud)
这是小提琴。
编辑
好的,我明白了,您想使用标记来在必要时手动设置位置。这是一个有效的解决方案:
您为所有例外国家定义了一个带有 latLng 的哈希表,这些国家的特征中心不是您想要的中心:
var exceptions = {
'France': [45.87471, 2.65],
'Spain': [40.39676, -4.04397]
}
Run Code Online (Sandbox Code Playgroud)
要显示标签,您可以在正确的位置放置一个不可见的标记,并为其绑定工具提示:
var label = L.marker(
exceptions[feature.properties.name] || layer.getBounds().getCenter(),
{
icon: L.divIcon({
html: '',
iconSize: [0, 0]
})
}
).addTo(map);
label.bindTooltip(
feature.properties.name,
{
permanent:true,
direction:'center',
className: 'countryLabel'
}
);
Run Code Online (Sandbox Code Playgroud)
这是另一个小提琴。