Search code examples
c#leaflet

Getting leaflet marker information from a click event


I have multiple of the following markers on my leaflet map. I wish that when I click on the marker that I can get the name that I set for the marker in "customValue". How do I do this? I understand that the e in "changeMapLineColour(e) " is for the event, how to I get marker information out?

Thank you very much :)

var volMarker;
var volMarkers = new Array();
function drawMapLabel(line, name) {

     volMarker = new L.marker([line.start.lat, line.start.lng], {
        draggable: true,
        autoPan: true,
        customValue: name
    }).bindTooltip(name,
        {
            permanent: true,
            direction: 'right'
        }).addTo(mymap).on('click', changeMapLineColour); 

    volMarkers.push(volMarker);
    return volMarker;
}

function changeMapLineColour(e) {        
    alert("hi. you clicked the marker at " NAMEHERE);
}

Solution

  • Using the above from Ivan. In a marker you can create your own custom values, below I have created 2: nameValue and userSelectedColour.

             vMarker = new L.marker([line.start.lat, line.start.lng], {
                draggable: true,
                autoPan: true,
                nameValue: name,
                userSelectedColour: selectedColour
            }).bindTooltip(name,
                {
                    permanent: true,
                    direction: 'right'
                }).addTo(mymap).on('click', changeMapLineColour); 
    

    Then to access the values use:

    function changeMapLineColour(e) {
    //some stuff then:
    lines[i].setStyle({
                        color: e.target.options.userSelectedColour,
                        weight: 3
    
    
                       })
    }