如何在Leaflet map多边形上触发事件?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (875)

我试图找出如何手动触发Leaflet多边形的事件(通过GeoJSON加载)。

我有一个有多个多边形的Leaflet地图。我也有一个常规的超链接,当它被点击时,应该触发特定多边形上的鼠标悬停事件(或任何事件)。

如何将ID分配给所有多边形,以便我可以将超链接绑定到特定的多边形事件?或者,甚至是这样做的最合乎逻辑的方式?

最终,我试图创建一个有多个多边形的地图,以及与每个多边形关联的HTML文本标签表。当点击HTML表格文本时,我想在地图多边形上触发事件(反之亦然)。我只是不知道如何引用每个多边形。

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>

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

});
提问于
用户回答回答于

你需要为每个打开弹出窗口的多边形创建一个单击事件,并为每个多边形分配一个唯一的ID,以便稍后可以引用它并手动触发其弹出窗口。

以下内容实现了这一点:

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

然后手动触发特定的图层单击事件,只需将其调用如下:

map._layers['polyindex0'].fire('click');
用户回答回答于
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); }

扫码关注云+社区

领取腾讯云代金券