RogerHN
RogerHN

Reputation: 604

Function to show custom content on marker popup

Before I was able to show in the PopUp the content of "pname" inside "icons.js", I managed to show that using this function. Now I added two more fields "dropname" and "dropicon", that I want to show under "pname". But something is wrong in my code. I don't know if the syntax is wrong or what, I know I am missing something in that function. Anyone can help me? My full code that uses that function is here:

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)
            }
        }
    }
}

Upvotes: 1

Views: 99

Answers (1)

Nikolay Ermakov
Nikolay Ermakov

Reputation: 5061

You have a couple of errors:

  1. you should replace lenght with length (multiple places)
  2. no dropname property in your icons.resources objects, but there is name property; maybe you should change it to icons.resources[i].name (multiple places)
  3. no dropicon property in your icons.resources objects, but there is icon property; maybe you should change it to icons.resources[i].icon (maybe multiple places)

Works here - http://plnkr.co/edit/8xgDc8zZYc6FVR6unAtf?p=preview

Upvotes: 1

Related Questions