1

I have generated the Leaflet web map using qgis2web plugin in qgis software. The map contain polygons. Now I want the polygons to be highlighted when I click on each and to stay highlighted until I click on another one. I want to know which part of code I should change. I have tried to turn on option "Highlight on hover" when I run qgis2web plugin but that polygon is highlighted just on hover not on click.

Note that I am using qgis 3.0 and the last version of qgis2web

asked Jun 19, 2018 at 8:21

1 Answer 1

2

This is largely answered in here: https://stackoverflow.com/questions/21029533/reset-style-on-leaflet-polygon-on-click#21032302. However, here's how to do it specifically in a qgis2web Leaflet export.

First, make sure "Highlight on hover" is checked when you export your map with qgis2web. Next, edit index.html from your exported map. Find the following code:

function highlightFeature(e) {
 highlightLayer = e.target;

Replace it with:

function highlightFeature(e) {
 if (highlightLayer) {
 for (i in e.target._eventParents) {
 e.target._eventParents[i].resetStyle(highlightLayer);
 }
 }
 highlightLayer = e.target;

Next find the following code:

layer.on({
 mouseout: function(e) {
 for (i in e.target._eventParents) {
 e.target._eventParents[i].resetStyle(e.target);
 }
 },
 mouseover: highlightFeature,
});

Replace it with this:

layer.on({
 click: highlightFeature,
});

That should do it.

answered Jun 19, 2018 at 11:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.