I'm working on a map application, and I need to link different events to different types of markers. The createMarkers function creates a few markers by random, but their overlay events always say exactly the same though their colors are different. This is built on Leaflet.js
function createMarkers() {
for( var i = 0; i < 10; i++ ) {
var color, kind;
switch(Math.floor(Math.random() * 4)) {
case 0:
color = greenFlag;
kind = "film";
break;
case 1:
color = redFlag;
kind = "music";
break;
case 2:
color = blueFlag;
kind = "images";
break;
case 3:
color = yellowFlag;
kind = "text";
break;
default:
}
var lat = Math.random() * 3 - 75;
var lng = Math.random() * 3 - 112;
var marker = L.marker([lat, lng], {icon: color}).addTo(map).on('click', function() {
overlay(kind);
});
}
}
function overlay(kind) {
alert(kind);
}
All the markers created have the same value when clicked, e.g. "film". I've tried storing the overlay(kind) in a variable inside the first switch statement and calling that. I've also tried creating a second switch statement in the marker.on('click') function but neither of these has solved the problem. I feel like I'm missing something pretty key about javascript/objects here.
Thank you!