JS传单:如何将(Geo-)json ID传递给点击事件? [英] JS leaflet: How to pass (Geo-)json ID to on click event?

查看:36
本文介绍了JS传单:如何将(Geo-)json ID传递给点击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 django web 应用程序应该执行以下操作:将 Geojson 对象传递给视图,使用传单映射点并在用户单击点标记时显示一些附加信息.我对 js 不太熟悉,所以我无法将正确的数据类型绑定到 click 事件.这是一个示例 geojson 对象.如何通过 click 事件访问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(feature.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传递给点击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆