Javascript 传单地图的第二个标签?
我正在尝试向传单地图添加第二个居中标签。这就是它目前的样子: 我使用对此的响应制作了当前的标签,下面是我的代码:Javascript 传单地图的第二个标签?,javascript,leaflet,label,maps,Javascript,Leaflet,Label,Maps,我正在尝试向传单地图添加第二个居中标签。这就是它目前的样子: 我使用对此的响应制作了当前的标签,下面是我的代码: var marker1 = L.marker([25.777085, -80.193935], {icon: redIcon}).addTo(mymap); var marker2 = L.marker([25.759461, -80.204921], {icon: redIcon}).addTo(mymap); createLabel(marker1, "Label
var marker1 = L.marker([25.777085, -80.193935], {icon: redIcon}).addTo(mymap);
var marker2 = L.marker([25.759461, -80.204921], {icon: redIcon}).addTo(mymap);
createLabel(marker1, "Label 1");
createLabel(marker2, "Label 2");
function createLabel(layer, text){
removeLabel(layer);
var icon = createStaticLabelIcon(text);
var testspan = document.createElement("span");
document.body.appendChild(testspan);
testspan.className = "textwidth";
testspan.style.fontSize = "10px";
testspan.innerHTML = text;
var width = testspan.clientWidth +11;
icon.options.iconAnchor = [width / 2, -4]; //That the label is centered
var label = L.marker(layer.getLatLng(),{icon: icon}).addTo(mymap);
layer.appendedLabel = label;
document.body.removeChild(testspan);
}
function removeLabel(layer){
if(layer.appendedLabel){
mymap.removeLayer(layer.appendedLabel); //Remove label that connected with marker, else the label will not removed
}
}
function createStaticLabelIcon(labelText) {
return L.divIcon({
className: "leaflet-marker-label",
html: '<span class="leaflet-marker-iconlabel" style="background: #CB2B3E; color: #FFFFFF;";>'+labelText+'</span>',
text : labelText,
});
}
任何关于如何在第一个标签下添加第二个标签的帮助,我都将不胜感激。下面是我想要的一个例子,尽管我不知道如何制作:
您需要更改的是y锚定并解除
removeLabel
功能:
icon.options.iconAnchor = [width / 2, -24]; //That the label is centered
这对于生成两个标签非常有效,但是如何为标签1和标签2设置不同的颜色?您需要添加与
createStaticLabelIcon
中相同的签入createLabel
并设置不同的背景色我在实现中遇到了问题:函数createStaticLabelIcon(labelText){var if(count==1){返回L.divIcon({className:“传单标记标签”,html:'+labelText+'',text:labelText,})}否则if(count==2){返回L.divIcon({className:“传单标记标签”,html:'+labelText+'',text:labelText,})};}
您还需要将count
变量传递给函数函数createStaticLabelIcon(labelText,count){…
-->var icon=createStaticLabelIcon(text,count)
添加计数
解决了这个问题,谢谢。我相信我使用的标签方法是你的。我在别处寻求帮助,但他们在标签方法上遇到了困难,因此,你能谈谈如何使这些标签在特定的缩放级别后显示吗
icon.options.iconAnchor = [width / 2, -24]; //That the label is centered
function createLabel(layer, text, count){
//removeLabel(layer);
var icon = createStaticLabelIcon(text);
var testspan = document.createElement("span");
document.body.appendChild(testspan);
testspan.className = "textwidth";
testspan.style.fontSize = "10px";
testspan.innerHTML = text;
var width = testspan.clientWidth +11;
var posY = 0;
if( count == 1){
posY = -4;
} else if( count == 2){
posY = -24;
}
icon.options.iconAnchor = [width / 2, posY]; //That the label is centered
var label = L.marker(layer.getLatLng(),{icon: icon}).addTo(mymap);
layer.appendedLabel = label;
document.body.removeChild(testspan);
}
createLabel(marker1, "Label 1.1",1);
createLabel(marker1, "Label 1.2",2);