I am working on a project that creates lines and I need to link each line with its corresponding marker on a Leaflet Map.
Sometimes, I am required to delete a line, hence I am required to delete a marker, and sometimes I need to interact with the marker directly.
For that purpose I added markerOptions and for testing purposes, I added a title, which I wish to alert, each time the marker is pressed.
On the creation method, I am able to access the title with no problems, the problem arise when I try to access the title from the onClick method.
I tried markerOptions.title; this.markerOptions.title; getMarkerOptions(title); this.getMarkerOptions(title), to no avail. I also looked at Leaflet website, and couldn't find an explanation how to achieve that. Additionally, I tried to send as a parameter to the onClick function the title, but apparently, the function does not accepts arguments besides the object that was clicked.
Below is my code for both methods.
Thanks in advance!
function addMarker(latitude, longitude, flightID) {
const markerOptions = {
title: markerIndex, //Naming the marker, according to the flightID
clickable: true, //Allowing clicks on the marker
draggable: false //Preventing dragging it.
}
const icon1 = L.icon({
iconUrl: '../path',
iconSize: [50, 32],
iconAnchor: [25, 16],
popupAnchor: [-3, -76]
});
markerID[markerIndex] = L.marker([latitude, longitude], //Initializing the marker with the cordinates of the flight.
{ icon: icon1 },
markerOptions).addTo(myMap).on('click', onClick); // Adding the marker to the map.
markerIndex++; //Generating a suffix for the next marker.
currentMarkers.push(markerID[markerIndex-1]); //Pushing the marker to the list of markers.
if ((highLightFlightID != null) && (highLightFlightID == flightID)) {
boldFlight(flightID);
}
return markerIndex;
}
function onClick(e) {
alert(this.markerOptions.title); //FAILS.
alert(this.getLatLng()); //WORKS.
highLightFlightID = null;
}