简体   繁体   中英

getting/setting unique id to a leafletjs circlemarker

I have a map with several markers on it. I construct these markers with this piece of code:

var markers = {},
    lbl = 'unique';

markers[lbl] = L.circleMarker(ll, 
      { radius: 8,  
        fillColor: '#ff0000',
        color: '#00ff00',
        weight: 0,
        opacity: 1,
        fillOpacity: 0.9,
        className: 'svgMarker'
       })
  .bindLabel('This is '+lbl)
  .addTo(markerLayer)
  .addTo(map)
  .on('click', clickHandler);;

Within the clickHandler I want to load some stuff depending on which marker I clicked. To distinguish them, I have a lbl (label)var which holds the unique alphanumeric ID of the marker.

function clickHandler(event){
   //- zoom to the marker
   map.setView(ev.latlng, 16);

   //- Load the marker dependent stuff.
   // how can I pass the unique label to this function?

}

Is there a way to pass the unique id with the mouse event or is there an other way to give a 'property' to the marker which I can read out in the clickHandler?

just add a property to your marker (just javascript stuff) ... you can get it back with context 'this' in the event handler

var marker = L.marker([48.8588589,2.3470599]);
marker.id = 'unique_id';
marker.addTo(map);

marker.on('click', clickHandler);

function clickHandler(event) {
     console.log(this.id);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM