Javascript JS传单:如何将(Geo-)json ID传递给点击事件?
我的django web应用程序应该执行以下操作:将Geojson对象传递到视图,使用传单映射点,并在用户单击点标记时显示一些附加信息。我对js不太熟悉,因此无法将正确类型的数据绑定到Javascript JS传单:如何将(Geo-)json ID传递给点击事件?,javascript,onclick,leaflet,geojson,Javascript,Onclick,Leaflet,Geojson,我的django web应用程序应该执行以下操作:将Geojson对象传递到视图,使用传单映射点,并在用户单击点标记时显示一些附加信息。我对js不太熟悉,因此无法将正确类型的数据绑定到clickevent。下面是一个示例geojson对象。如何使用我的单击事件访问“id” var geojsonFeature = {'geometry': {'type': 'MultiPoint', 'co
click
event。下面是一个示例geojson对象。如何使用我的单击事件访问“id”
var geojsonFeature = {'geometry':
{'type': 'MultiPoint',
'coordinates': [[4.939, 52.33], [4.9409, 52.33]]
},
'type': 'Feature',
'properties':
{'id': '52','popupContent': 'id=52'}
};
将geojson对象添加到地图
var gj = L.geoJson(geojsonFeature, {
pointToLayer: function (feature, latlng) {
return L.circleMarker(latlng, geojsonMarkerOptions);
}}).addTo(map);
然后()上的,
-单击
gj.on('click', function(evt) {
alert(id) // well, this is where I need help
});
注意:我不想使用像bindPopup(feature.properties.popupContent)
这样的东西,因为我想打开一个新窗口,用数据库中的一些额外数据调用另一个django视图。对于每个有类似问题的人:你想使用的是onEachFeature
函数。该特性表示geojson对象。使用上面提供的示例数据,可以通过feature.properties.popupContent
访问id
function onEachFeature(feature, layer) {
layer.on('click', function (e) {
alert(feature.properties.popupContent);
//or
alert(feature.properties.id);
});
}
在尝试上面发布的解决方案但未成功后,我使此版本正常工作:
function onEachFeature(feature, layer) {
layer.on('click', function (e) {
alert(e.target.feature.properties.popupContent);
//or
alert(e.target.feature.properties.id);
});
}
我爱你,伙计,经过一个晚上的搜索,我得到了这个答案。