【发布时间】:2017-10-22 18:00:07
【问题描述】:
我有一张传单地图,通过数据库中的两个 url 提供数据。我使用 django 和 django-leaflet。 python 3.5,postgresql 9.6。
在加载页面时显示layerA。打开 layerA 的弹出窗口我希望可以通过单击 LayerA 中项目的弹出窗口来加载 layerB。 LayerB 中的项属于 LayerA 中的某些项,通过外键连接。
这是一个 jsfiddle: https://jsfiddle.net/zt56nmog/11/
这是我的代码:
function map_init_basic (map, options) {
urlA = "http://127.0.0.1:8000/data.A" // Geojson
urlB = "http://127.0.0.1:8000/data.B" // Geojson
layerA = L.geoJson(null, {
pointToLayer: function(feature, latlng) {
return L.marker(latlng, {icon: hvIcon});
},
onEachFeature: function( feature, layer) {
popupText = "button to call layerA" + "<button id='theirFlats' type='button' class='btn btn-link' onclick='getLayerB(\""+feature.id+"\");'>show layerB:</button>";
layer.bindPopup(popupText);
}
});
layerB = L.geoJson(null, {
filter: function(feature, layer) {
{return feature.properties.id_hv == id_layerA;};
},
pointToLayer: function(feature, latlng) {
return L.marker(latlng, {icon: whgIcon});
}
});
$.getJSON(urlA, function(data){
layerA.addData(data);
});
layerA.addTo(map);
} // end map_init
function getLayerB(id_layerA){
alert(id_layerA); // alerts the right id
layerA.remove(); // works, removes the layerA
alert(urlB)
$.getJSON(urlB, function(data){
layerB.addData(data);
console.log(data);
});
layerB.addTo(map);
alert(layerB)
map.fitBounds(layerB);
}
我收到以下错误:
TypeError: t 未定义
【问题讨论】:
-
错误说你没有声明id_layerB变量,请检查代码,因为你真的错过了。
-
哦,对不起,是的,这是一个典型的问题。意味着是 id_layerA。但另一个错误在那里,我不知道 t 是什么。
-
你能为这些情况编写 jsfiddle 吗?调试和验证想法会更容易
-
jsfiddle.net/zt56nmog/13 感谢您的关注
-
我已将功能添加到窗口,一切都开始工作了
window.getLayerB = getLayerB;jsfiddle.net/2bufypa2/1
标签: javascript jquery django leaflet layer