3

My site consists of a Leaflet map with the leaflet.markerclusters plugin. I am also using Flowplayer to play a video that opens in a JQuery Tools overlay using the selector id "#video1".

Currently, when I click on any marker on the map it fires my test video in an overlay. My goal is to create a click event unique to each individual marker in the cluster. Eventually, I would like every marker to have a click event that fires a video unique to that marker.

I am a beginner, and have been doing okay using these well documented libraries up until now. However, I don't have the skills to bridge this current gap. Would someone please give me a push in the right direction? Below is a link to my JS Fiddle. My issue begins on JavaScript line 2098.

 var markers = new L.MarkerClusterGroup();

var addressPoints = [
    [40.634902, -73.965054, "Video1"],
    [40.660897, -73.961082, "Video2"],
    [40.693353, -73.970413, "Video3"],
    [40.693289, -73.966289, "Video4"],
    [40.68973, -73.971007, "Video5"],
    [40.718423, -73.957428, "Video6"],
    [40.71817, -73.956918, "Video7"],
    [40.681427, -73.993959, "Video8"]
];

for (var i = 0; i < addressPoints.length; i++) {
    var a = addressPoints[i];
    var title = a[2];
    var marker = new L.Marker(new L.LatLng(a[0], a[1]), {
        title: title
    });
    marker.bindPopup(title);
    markers.addLayer(marker);
}

map.addLayer(markers);

//assign video div ID to overlay
$('#video1').overlay({
    load: false,
    top: "center",
    left: "center"
});

//bind marker click event to overylay
 markers.on('click', function () {
    $("#video1").data("overlay").load();
});

Thank you, Joey

http://jsfiddle.net/Joey84/nM458/26/

user2163278
  • 31
  • 1
  • 2

1 Answers1

6

You are headed in the right direction with the markers.on("click"... function. You just need to make a few edits. Just as you added the event listener to the entire "markers" layer, you can add it to individual markers in your for loop.

...

for (var i = 0; i < addressPoints.length; i++) {
    var a = addressPoints[i];
    var title = a[2];
    var marker = new L.Marker(new L.LatLng(a[0], a[1]), {
        title: title
    });
    if (title=="Video1") {
        marker.on('click', function () {
            $("#video1").data("overlay").load();
        });
    }
    marker.bindPopup(title);

    markers.addLayer(marker);
}
...

Likewise - and probably the better solution - you can access details about the marker you clicked in the on("click"... you are currently using by passing a variable to the function. This would be useful if you have multiple videos and don't want to check with an if statement when creating markers.

 markers.on('click', function (d) {
       // Grab marker title and make it look like an ID
       var marker_title = "#" + d.layer.options.title.toLowerCase();
       // Make sure the jQuery object exists
       if ( $(marker_title) ){
           // Call up the video.
           $(marker_title).data("overlay").load();
       }
   });

Note that I used toLowerCase() because your data has the title capitalized and the video id is all lowercase.

Here it is in action: http://jsfiddle.net/nM458/44/

patsweet
  • 1,548
  • 10
  • 12