最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reset style on leaflet polygon on click - Stack Overflow

programmeradmin2浏览0评论

I have a map made with leaflet.js with a geoJSON layer consisting of 70-some polygons. Each time the user clicks on a polygon, it is highlighted and a side panel is filled with data and opened:

function clickFeature(e) {
    var layer = e.target;
        layer.setStyle({
        weight: 3,
        color: '#666',
        dashArray: '',
        fillOpacity: 0.7
    });

    info.update(layer.feature.properties);
    $( "#mypanel" ).panel("open");
}

That works fine. But I need to change it so that each time a polygon is clicked, it simultaneously gets highlighted AND the previously clicked polygon returns to the original style, so only one polygon is ever "selected" at a time.

I've tried this, but it doesn't work (the panel is no longer updated or opened):

var lastClickedLayer;
function clickFeature(e) {
    geojson.resetStyle(lastClickedLayer);
    var layer = e.target;
        layer.setStyle({
        weight: 3,
        color: '#666',
        dashArray: '',
        fillOpacity: 0.7
    });

    info.update(layer.feature.properties);
    $( "#mypanel" ).panel("open");
    layer = lastClickedLayer;
}

Any help very appreciated.

I have a map made with leaflet.js with a geoJSON layer consisting of 70-some polygons. Each time the user clicks on a polygon, it is highlighted and a side panel is filled with data and opened:

function clickFeature(e) {
    var layer = e.target;
        layer.setStyle({
        weight: 3,
        color: '#666',
        dashArray: '',
        fillOpacity: 0.7
    });

    info.update(layer.feature.properties);
    $( "#mypanel" ).panel("open");
}

That works fine. But I need to change it so that each time a polygon is clicked, it simultaneously gets highlighted AND the previously clicked polygon returns to the original style, so only one polygon is ever "selected" at a time.

I've tried this, but it doesn't work (the panel is no longer updated or opened):

var lastClickedLayer;
function clickFeature(e) {
    geojson.resetStyle(lastClickedLayer);
    var layer = e.target;
        layer.setStyle({
        weight: 3,
        color: '#666',
        dashArray: '',
        fillOpacity: 0.7
    });

    info.update(layer.feature.properties);
    $( "#mypanel" ).panel("open");
    layer = lastClickedLayer;
}

Any help very appreciated.

Share Improve this question edited Jan 31, 2019 at 8:55 kboul 14.6k5 gold badges47 silver badges58 bronze badges asked Jan 9, 2014 at 19:36 wbendwbend 551 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Your assignment is wrong, it has to be

lastClickedLayer = layer;

You also should add an additionally check if lastClickedLayer has already been set:

if(lastClickedLayer){
   geojson.resetStyle(lastClickedLayer);
}
发布评论

评论列表(0)

  1. 暂无评论