I am using Leaflet in order to render a map. I created a map with markers and I don't know how to implement the Event Listener 'onClick' on each Marker.
My code
var stops = JSON.parse(json);
var map = new L.Map('map', {
zoom: 12,
minZoom: 12,
center: L.latLng(41.11714, 16.87187)
});
map.addLayer(L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
attribution: 'Map data',
maxZoom: 18,
id: 'mapbox.streets',
accessToken: '-----'
}));
var markersLayer = new L.LayerGroup();
map.addLayer(markersLayer);
//populate map from stops
for (var i in stops) {
L.marker(L.latLng(stops[i].Position.Lat, stops[i].Position.Lon), {
title: stops[i].Description
}).addTo(markersLayer).bindPopup("<b>" + stops[i].Description + "</b>").openPopup();
}
Example
map.on('click', function(e) {
alert(e.latlng);
});
Leaflet deals with event listeners by reference, so if you want to add a listener and then remove it, define it as a function:
function onClick(e) { ... }
map.on('click', onClick);
map.off('click', onClick);