如何更新Leaflet弹出窗口中的内容?

Mik*_*ltz 6 javascript maps geojson leaflet

我正在使用Wlet with Leaflet.我正在建立一张美国地图,使用传单的L.GeoJSON用GeoJSON绘制州界.

我可以在地图加载期间设置所有内容,但是我需要能够在绘制地图后调整弹出窗口中的内容.这是我正在做的精简版:

var gjStates = new L.GeoJSON(null, null);
wax.tilejson(url, function(tilejson) {  
    map = new L.Map('map').addLayer(new wax.leaf.connector(tilejson)).addLayer(gjStates);

    gjStates.on("featureparse", function (e) {
        if (e.properties && e.properties.name){
            pops[e.id.substring(4)] = e.layer.bindPopup('<h4>Hello ' + e.properties.name + '!</h4>');
        }
    });
    for (s in usStateData) {
        gjStates.addGeoJSON(usStateData[s]);
    }
});
Run Code Online (Sandbox Code Playgroud)

现在,一切都很好,弹出窗口很好,但后来我想改变它,没有办法引用它.我在源代码中看到bindPopup()返回'this',我认为它是L.Popup对象,但结果是其他东西.因此,例如,以下代码将使用活动弹出窗口,而不是特定的L.Path对象(状态)我想要获取的特定窗口.

pops['AK']._map._popup.setContent('I am ALASKA!');
Run Code Online (Sandbox Code Playgroud)

使用firebug挖掘DOM,我还可以看到弹出内容是在内部变量中设置的,我可以更新它.但是,更新这个并不会更新HTML,而且我无法找出阿拉斯加有52的密钥._layers [52]也没有我希望的setContent()方法,如果它是一个L .Popup对象.

gjStates._layers[52]._popupContent = 'I am ALASKA!';
Run Code Online (Sandbox Code Playgroud)

所以,我有点卡住而没找到我需要的东西.在初始渲染后,我有什么方法可以引用并更新地图上特定弹出窗口的内容吗?

Aco*_*orn 1

我很确定您可以保留对所有图层的引用,然后layer.bindPopup()稍后调用以更改弹出内容。