JS传单:如何将(Geo-)json ID传递给click事件? [英] JS leaflet: How to pass (Geo-)json ID to on click event?
问题描述
我的django网络应用应该执行以下操作:将Geojson对象传递到视图,使用传单映射点,并在用户单击点标记时显示一些其他信息。我对js不是那么熟悉所以我被绑定了正确类型的数据到点击
事件。这是一个geojson对象示例。如何使用点击
事件来访问id?
My django web app should do the following: Pass a Geojson object to a view, map the points with leaflet and display some additional information when the user clicks on a point marker. I'm not so familiar with js so I got stuck binding the right kind of data to click
event. Here is a sample geojson object. How can I access the 'id' with my click
event?
var geojsonFeature = {'geometry':
{'type': 'MultiPoint',
'coordinates': [[4.939, 52.33], [4.9409, 52.33]]
},
'type': 'Feature',
'properties':
{'id': '52','popupContent': 'id=52'}
};
将geojson对象添加到地图中..
Adding the geojson object to the map..
var gj = L.geoJson(geojsonFeature, {
pointToLayer: function (feature, latlng) {
return L.circleMarker(latlng, geojsonMarkerOptions);
}}).addTo(map);
on()
-click。 ...
And the on()
-click....
gj.on('click', function(evt) {
alert(id) // well, this is where I need help
});
注意:我不想使用类似 bindPopup(功能。 properties.popupContent)
因为我想打开一个新窗口,用一些来自数据库的额外数据调用不同的django视图。
NOTE: I don't want to use something like bindPopup(feature.properties.popupContent)
because I want to open a new window that calls a different django view with some extra data from the database.
推荐答案
对于遇到类似问题的每个人:你想要使用的是 onEachFeature
函数。该功能表示geojson对象。使用上面提供的示例数据,可以通过 feature.properties.popupContent
访问id。
For everyone with a similar problem: What you want to use is the onEachFeature
function. The feature represents a geojson object. Using the sample data provided above the id can be accessed through feature.properties.popupContent
.
function onEachFeature(feature, layer) {
layer.on('click', function (e) {
alert(feature.properties.popupContent);
//or
alert(feature.properties.id);
});
}
这篇关于JS传单:如何将(Geo-)json ID传递给click事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!