Search code examples
leaflethighlightpolyline

leafletjs : Highlight polyline on mouseover


I have an array of polylines that displayed on a map, now what I am aiming to do is, when I hover over a certain polyline from the list, only that polyline highlights (or changes color).

What I have right now is something like this (this code is inside a loop that goes to the end filling polyLineArray with individual polyline data,

var pointList = [];

// pointList is an array and lat/lngs

var polyLineProperties = {
    color: 'red',
    opacity: 1,
    weight: 5,
    clickable: true
}

var polyLine = new L.polyline(pointList, polyLineProperties);
polyLine.on('mouseover', function() {
    // WHAT TO DO HERE to HIGHLIGHT that specific polyline.
});

polyLineArray.push(polyLine);

How to alter any property of a polyline and not just color?


Solution

  • Okay,

    Sorry but I've managed to figure this one out, thanks to the tutorial on the following link,

    Interactive Choropleth Map

    This is all that was required,

    polyLine.on('mouseover', function(e) {
        var layer = e.target;
    
        layer.setStyle({
            color: 'blue',
            opacity: 1,
            weight: 5
        });
    });
    

    Thank you all for reading.