Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger events on Leaflet map polygons?

Tags:

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
});

});
like image 685
Critter Avatar asked Jul 10 '12 20:07

Critter


People also ask

How do you know if a point is inside a polygon leaflet?

Click on the map or the markers and a pop-up will show indicating if the point you clicked on, or the marker, is in the polygon. Leaflet.

Why is map not showing in leaflet?

There are a number of reasons why your map might not be displaying: You have an error in your JavaScript (most likely) - use whichever debugging tool is provided with your browser to verify. you are using Internet Explorer and you have Compatibility mode ON....


3 Answers

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!

like image 57
Critter Avatar answered Sep 25 '22 20:09

Critter


So, quick update.

Just call fireEvent (or its alias fire) on whatever layer you need.

You don't need to risk ._private[Vars], just get a reference to the target layer and fire away, e.g.

var vectorLayer = map.getLayer('myVectorLayer');
vectorLayer.fire('click');
like image 43
danwild Avatar answered Sep 23 '22 20:09

danwild


function clickMarker(i){
var popupContent = "content here or html format",
popup = new L.Popup({offset:new L.Point(0,-28)});

popup.setLatLng(LatLng);
popup.setContent(popupContent);
map.panTo(LatLng);
map.openPopup(popup); }

i = got a corresponding coordinate which is LatLng

like image 36
uno2xx Avatar answered Sep 24 '22 20:09

uno2xx