Search code examples
eventsdictionarygeojsonleaflet

How to trigger events on Leaflet map polygons?


I'm trying to figure out how to manually trigger events for Leaflet polygons (loaded via GeoJSON).

In a nutshell, I have a Leaflet map with numerous polygons. I also have a regular hyperlink outside of the map that when clicked, should trigger a mouseover event (or any event really) on a particular polygon.

How do I assign ID's to all of my polygons so that I can bind hyperlink(s) to a specific polygon's event? Or is that even the most logical way of doing this?

Ultimately, I'm trying to create a map with numerous polygons along with an HTML table of text labels that are associated to each polygon. When clicking on the HTML table text, I'd like to trigger events on the map polygons (and vice versa). I just don't know how to reference each polygon.

Here is my very simplified HTML:

<body>

    <div id="map" style="height: 550px; width:940px"></div>

    <a href="#" id="testlink">Click to trigger a specific polygon mouseover event</a>

</body>

Here is my very simplified JS:

$(document).ready(function () {

// build the map and polygon layer
function buildMap(data) {

    var map = new L.Map('map');

    var cloudmadeUrl = 'http://{s}.tile.cloudmade.com/***yourkeyhere***/66267/256/{z}/{x}/{y}.png',
        cloudmadeAttribution = '',
        cloudmade = new L.TileLayer(cloudmadeUrl, {maxZoom: 18, attribution: cloudmadeAttribution});

    var mapLoc = new L.LatLng(43.675198,-79.383287);
    map.setView(mapLoc, 12).addLayer(cloudmade);

    var geojsonLayer = new L.GeoJSON(null, {});

    geojsonLayer.on("featureparse", function (e){

        // apply the polygon style
        e.layer.setStyle(polyStyle);

        (function(layer, properties) {
            layer.on("mouseover", function (e) {
                // change the style to the hover version
                layer.setStyle(polyHover);
                });
            });
            layer.on("mouseout", function (e) {
                // reverting the style back
                layer.setStyle(polyStyle);
            });
            layer.on("click", function (e) {
                // do something here like display a popup
                console.log(e);
            });
        })(e.layer, e.properties);

    });

    map.addLayer(geojsonLayer);

    geojsonLayer.addGeoJSON(myPolygons);    

}

// bind the hyperlink to trigger event on specific polygon (by polygon ID?)
$('#testlink').click(function(){
    // trigger a specific polygon mouseover event here
});

});

Solution

  • OK, I've figured it out.

    You need to create a click event for each polygon that opens the popup, and assign a unique ID to each polygon so you can reference it later and manually trigger its popup.

    The following accomplishes this:

    var polyindex = 0;
    
    popup = new L.Popup();
    
    geojsonLayer = new L.GeoJSON(null, {});
    
    geojsonLayer.on("featureparse", function (e){
    
        (function(layer, properties) {
    
            //click event that triggers the popup and centres it on the polygon
            layer.on("click", function (e) {
                var bounds = layer.getBounds();
                var popupContent = "popup content here";
                popup.setLatLng(bounds.getCenter());
                popup.setContent(popupContent);
                map.openPopup(popup);
            });
    
        })(e.layer, e.properties);
    
        //assign polygon id so we can reference it later
        e.layer._leaflet_id = 'polyindex'+polyindex+'';
    
        //increment polyindex used for unique polygon id's
        polyindex++;
    });
    
    //add the polygon layer
    map.addLayer(geojsonLayer);
    geojsonLayer.addGeoJSON(neighbourhood_polygons);
    

    Then to manually trigger a specific layers click event, simply call it like this:

    map._layers['polyindex0'].fire('click');
    

    Everything between the square brackets is the unique ID of the layer you want to trigger. In this case, I'm firing the click event of layer ID polyindex0.

    Hope this info helps somebody else out!