【发布时间】:2018-08-07 21:42:37
【问题描述】:
当我单击某个 HTML 元素时,我使用Leaflet JavaScript API 创建一个弹出窗口。问题是弹出窗口不会显示在我的地图上。我不确定我的代码有什么问题。
以下是 json 文件中的功能示例。请注意,为方便起见,我将整个 json 对象分配为 var dataset。
var dataset = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"stemburo": "Sporthal Nieuwland",
"aantal_stemmen": 3753,
"lat": 52.2006665,
"lng": 5.3758691,
"Actief": 34,
"Amersfoort 2014": 348,
"Blanco lijst Chel": 5,
"Burger Partij Amersfoort": 258,
"Christen Democratisch App\u00e9l": 556,
"ChristenUnie": 350,
"DENK": 117,
"Democraten 66": 525,
"GroenLinks": 345,
"Partij van de Arbeid": 239,
"Socialistische Partij": 189,
"Staatkundig Gereformeerde Partij": 42,
"Volkspartij voor Vrijheid en Democratie": 717,
"Vrede en Recht": 28
},
"geometry": {
"type": "Point",
"coordinates": [
5.3758691,
52.2006665
]
}
}
下面是我创建 HTML 元素的 JavaScript 代码的一部分。我使用 JQuery 的$.each 来读取数据集。我将 'stemburo' 属性值(来自 json 对象)分配为元素的 id 属性。
当点击一个元素时,它会检查点击的 id 值是否与properties.stemburo 值之一相同。随后,应根据属性的坐标显示一个弹出窗口。
$.each(dataset.features,function(key,val){
var stemlocaties =val.properties.stemburo;
var newElement= document.createElement('a');
newElement.id= stemlocaties;
newElement.className='list-group-item list-group-item-action';
newElement.innerHTML=stemlocaties;
document.getElementById('stemlocaties').appendChild(newElement);
newElement.onclick=function(){
if (val.properties.stemburo===this.id){
var lng = val.geometry.coordinates[0];
var lat = val.geometry.coordinates[1];
L.popup()
.setLatLng([lat,lng])
.setContent('New popup text!')
.openOn(map);
}
};
});
我还是 JavaScript 新手。对于任何反馈,我们都表示感谢。
【问题讨论】:
标签: javascript html leaflet