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

I have a map made with leaflet.js with a geoJSON layer consisting of 70-some polygons. Each time the us

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);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744709535a4589258.html

相关推荐

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

    I have a map made with leaflet.js with a geoJSON layer consisting of 70-some polygons. Each time the us

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信