在我能够在PopUp中显示“icons.js”中“pname”的内容之前,我设法显示使用此功能。现在我又添加了两个字段“dropname”和“dropicon”,我想在“pname”下显示。但是我的代码出了点问题。我不知道语法是错还是什么,我知道我错过了那个函数。 有人可以帮帮我吗? 我在这里使用该函数的完整代码:
http://plnkr.co/edit/ZiRgOuodGYyr0Rsl6PZk?p=preview
function showResourcesByName(name) {
for (var i = 0; i < markers.resources.length; i++) {
var resName = markers.resources[i].name;
if (resName == name) {
var resIcon = icons.resources[i].icon;
var resSize = icons.resources[i].size;
var resPname = icons.resources[i].pname;
var resDropName = icons.resources[i].dropname;
var resDropIcon = icons.resources[i].dropicon;
alert (resDropName.lenght);
var popupContent = '<span class="markername">'+resPname+'</span><span class="drop">Drops:</span><div class="dropimgs">';
var popupContentDrops;
for (var dropindex = 0; dropindex < icons.resources[i].dropname.lenght; dropindex++) {
popupContentDrops += '<img src="'+resDropIcon[dropindex]+'"><span class="dropimgtext">'+resDropName[dropindex]+'</span>';
}
popupContent += popupContentDrops + '</div>';
var customIcon = L.icon({
iconUrl: resIcon,
iconSize: resSize, // size of the icon
iconAnchor: [resSize[0]/2, resSize[1]/2], // point of the icon which will correspond to marker's location
popupAnchor: [2, -resSize[1]/2] // point from which the popup should open relative to the iconAnchor
});
for (var j = 0; j < markers.resources[i].coords.length; j++) {
var x = markers.resources[i].coords[j].x;
var y = markers.resources[i].coords[j].y;
marker = L.marker([y, x], {icon: customIcon});
marker.addTo(map).bindPopup(popupContent);
$(marker._icon).addClass(name)
}
}
}
}
答案 0 :(得分:1)
你有几个错误:
lenght
替换为length
(多个地方)dropname
个对象中icons.resources
属性,但有name
属性;也许你应该把它改成icons.resources[i].name
(多个地方)dropicon
个对象中icons.resources
属性,但有icon
属性;也许你应该把它改成icons.resources[i].icon
(可能是多个地方)