【问题标题】:How to trigger events on Leaflet map polygons?如何触发传单地图多边形上的事件?
【发布时间】:2012-07-10 09:22:36
【问题描述】:

我正在尝试弄清楚如何手动触发 Leaflet 多边形的事件(通过 GeoJSON 加载)。

简而言之,我有一张带有许多多边形的传单地图。我在地图外还有一个常规超链接,当点击它时,应该会在特定多边形上触发鼠标悬停事件(或任何事件)。

如何为所有多边形分配 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
});

});

【问题讨论】:

    标签: events map geojson leaflet


    【解决方案1】:

    好的,我想通了。

    您需要为打开弹出窗口的每个多边形创建一个点击事件,并为每个多边形分配一个唯一的 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');
    

    方括号之间的所有内容都是您要触发的图层的唯一 ID。在这种情况下,我触发了图层 ID polyindex0 的点击事件。

    希望此信息对其他人有所帮助!

    【讨论】:

    • 有没有办法在不深入研究背景属性的情况下做到这一点?
    • 我认为这需要使用新的 API 进行更新,featureparse 似乎没有触发并且 addGeoJSON 似乎不是一个函数
    • 非常感谢,我正在寻找类似的东西.. map._layers['polyindex0'].fire('click');
    【解决方案2】:

    所以,快点更新吧。

    只需在您需要的任何层上调用fireEvent(或其别名fire)。

    您无需冒险 ._private[Vars],只需获取对目标层的引用并开火,例如

    var vectorLayer = map.getLayer('myVectorLayer');
    vectorLayer.fire('click');
    

    【讨论】:

      【解决方案3】:
      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 = 得到一个对应的坐标 LatLng

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-22
        • 2022-01-09
        • 2012-11-28
        • 2020-10-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多