1

I made a map with leaflet by following the official Leaflet tutorial on interactive choropleth maps.

I would like to make the map compatible with smartphones, so I added a click event, which acts like hovering on a feature.

function onEachFeature(feature, layer) {
layer.on({
    mouseover: highlightFeature,
    mouseout: resetHighlight,
    click: highlightFeature
});
}

The consequence: When a user is clicking, the hightlightFeatures remain highlighted.

Is there a way to prevent this? In other words: Is there an event on the lines of mouseout, in order to activate resetHighlight by clicking another feature?

1

The highlightFeature from the choropleth example can be changed to :

function highlightFeature(e) {
    geojson.setStyle(style); // This line will reset the style of all the features.
    var layer = e.target;

    layer.setStyle({
        weight: 5,
        color: '#666',
        dashArray: '',
        fillOpacity: 0.7
    });

    if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) {
        layer.bringToFront();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.