宣传单负荷数据和编辑功能,性能上点击鼠标宣传单、负荷、点击鼠标、性能

2023-09-11 01:32:39 作者:有个疯子陪涐笑

我加载从GeoJSON文件中的地图数据和附加click事件,每polygone。上点击,脚本应该从服务器获取数据并修改点击多边形的属性之一。即:

I am loading map data from a GeoJSON file and attaching a click event for every polygone. on click, the script should fetch data from the server AND modify one of the clicked polygon's properties. i.e:

function onClick(e) {
    var status = e.target.feature.properties.ACCESS;
    $.ajax({
        url: "http://127.0.0.1:8080/?&u=x&p="+e.target.feature.properties.ID_PARCELL,
        dataType: 'jsonp',
        type: 'GET',
        success: function(data) {
        status = data.status;
        e.target.feature.properties.ACCESS = data.status;
        e.target.bindPopup("Owner: <b>"+ data.status +"</b>").openPopup();
        },
        error: function(data){console.log(data)}
    });
    e.target.feature.properties.ACCESS = status;
    map.fitBounds(e.target.getBounds());
}

不过,由于成功的功能是一个回调(同步与否,并不重要),我不能够回到原来的事件源(即电子),所以我可以修改它的属性之一。

But since the success function is an callback (synchronous or not, it doesn't really matter), I am not able to get back to the original event source (namely e) so I could modify one of its properties.

我的问题是:我怎样才能回到事件源加载这些数据后?有没有通用的Javascript的方法是什么?如果没有,有没有什么办法可以查询GeoJSON一层要素ID? (=>我因此可以发送功能ID在Ajax调用,并简单地拿回来的响应)

My question is: How can I get back to the event source after loading this data? Is there any generic Javascript way? If not, is there any way I can query the GeoJSON layer by feature ID ? (=> I can therefore send the feature ID in the ajax call, and simply get it back with the response)

推荐答案

解决方案是使用上下文项所需的变量发送电子匿名函数:

The solution is to send the desired variable e to the anonymous function by using the context entry:

function onClick(e) {
    var status = e.target.feature.properties.ACCESS;
    $.ajax({
        url: "http://127.0.0.1:8080/?&u=x&p="+e.target.feature.properties.ID_PARCELL,
        dataType: 'jsonp',
        context: e,
        type: 'GET',
        success: function(data) {
        status = data.status;
        e.target.feature.properties.ACCESS = data.status;
        e.target.bindPopup("Owner: <b>"+ data.status +"</b>").openPopup();
        },
        error: function(data){console.log(data)}
    });
    e.target.feature.properties.ACCESS = status;
    map.fitBounds(e.target.getBounds());
}