【问题标题】:onEachFeature is not calledonEachFeature 没有被调用
【发布时间】:2019-06-25 09:56:25
【问题描述】:

我正在尝试添加一些带有特定样式和弹出窗口的标记,但如果我使用 PointToLayer 函数,则不会调用 onEachFeature。所以我不能添加弹出窗口。

如果我只使用onEachFeature,我可以使用console.log(feature),但我不能显示标记。如果我使用 pointToLayer,则不会调用 onEachFeature。

var json_chambre = L.geoJson(response, {

    pointToLayer: function(feature, latlng) {
        var markerCh = L.circleMarker(latlng, {
            radius: 5,
            fillColor: "#ff7800",
            color: "#000",
            weight: 1,
            opacity: 1,
            fillOpacity: 0.8
        });
        chambre_pit.addLayer(markerCh);
    },
    onEachFeature: function(feature, layer) {
        console.log(feature);
    }
}); 

没有错误,只是我不能同时拥有弹出窗口和样式。

【问题讨论】:

    标签: javascript leaflet markers


    【解决方案1】:

    让我引用Leaflet reference for the pointToLayer callback option

    Function 定义 GeoJSON 点如何生成 Leaflet 层。它在添加数据时在内部调用,传递 GeoJSON 点特征及其LatLng。默认是生成一个默认的Marker

    function(geoJsonPoint, latlng) {
        return L.marker(latlng);
    }
    

    注意这和你的代码有什么不同? pointToLayer 回调返回L.Marker 的实例,并将其添加到L.GeoJson 实例(L.LayerGroup 的子类)中。

    由于您不返回标记(或层)实例,L.GeoJson 实例最终为空,onEachFeature 循环通过总共零个特征+层对。

    另请注意,您不需要在onEachFeature 步骤附加弹出窗口,即:

    var json_chambre = L.geoJson(response, {
        pointToLayer: function(feature, latlng) {
            var markerCh = L.circleMarker(latlng, {
                radius: 5,
                fillColor: "#ff7800",
                color: "#000",
                weight: 1,
                opacity: 1,
                fillOpacity: 0.8
            });
            markerCh.bindPopup(feature.properties.label); // Or whatever
            return markerCh; // And do return the marker so it gets added to json_chambre.
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 2019-03-31
      • 2012-12-16
      • 2015-03-28
      相关资源
      最近更新 更多