LeafletJS markers move on zoom

The solution to this is quite simple. Leads should have posted it.

when your markers are moving around your map it’s because the map doesn’t know the size of your marker and/or it doesn’t know the point of your marker that marks the location.

your marker icon code might look like this:

var locationIcon = L.icon({iconUrl:'location_marker_icon.png'});

now, let’s suppose your image is 24px wide and 36px tall. To keep your marker from moving around, you simply specify the size of the marker, and the “anchor point”…

var locationIcon = L.icon({
    iconUrl:'location_marker_icon.png',
    iconSize: [24,36],
    iconAnchor: [12,36]
});

This will make the center pixel on the bottom represent the exact lat/lng point you specified the marker for, and it will keep it anchored there!

Leave a Comment