在标记弹出窗口上显示自定义内容的功能

时间:2016-02-07 02:53:10

标签: javascript function leaflet var marker

在我能够在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)
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你有几个错误:

  1. 您应该将lenght替换为length(多个地方)
  2. 您在dropname个对象中
  3. 没有icons.resources属性,但有name属性;也许你应该把它改成icons.resources[i].name(多个地方)
  4. 您在dropicon个对象中
  5. 没有icons.resources属性,但有icon属性;也许你应该把它改成icons.resources[i].icon(可能是多个地方)
  6. 在这里工作 - http://plnkr.co/edit/8xgDc8zZYc6FVR6unAtf?p=preview