Atlaslog / Field Notes / Styling & rendering
Field note · Styling & rendering
Pin tip misses the coordinate by 12 pixels
Quick answer
A 24x40 pin whose tip is at bottom center points about 20 px south of the plotted coordinate. Leaflet's default `iconAnchor` is the icon center, `[12, 20]`, so the coordinate lands in the middle of the image instead of at the tip.
Symptom
- At z18 the pin tip points about 24 m south of `116.397428, 39.909230`
- The offset stays a constant pixel amount at every zoom, so it is not a projection error
- `img.naturalWidth` and `img.naturalHeight` are `24` and `40` while `iconAnchor` was never set
Checklist
- Measure the icon's intrinsic size and the tip's pixel position inside it
- Check whether `L.icon` specifies `iconAnchor`
- Place a 1x1 test marker at the same coordinate and compare it with the pin tip
- Check CSS transforms or margins applied to `.leaflet-marker-icon`
- Verify `popupAnchor` matches the new anchor
Fix
Set `iconAnchor: [12, 40]` for a 24x40 pin with its tip at bottom center and `popupAnchor: [0, -40]`; for a `divIcon`, align the anchor with the tip element (`iconSize: [20, 32], iconAnchor: [10, 32]`).
Code
const img = document.querySelector('.leaflet-marker-icon');
console.log(img.naturalWidth, img.naturalHeight); // 24 40
// 针尖应在 (12, 40),若锚点为 (12, 20),向下偏 20 像素const pin = L.icon({
iconUrl: '/assets/img/pin.png',
iconSize: [24, 40],
iconAnchor: [12, 40], // 针尖在底部中央
popupAnchor: [0, -40] // 弹窗出现在图钉上方
});
L.marker([39.909230, 116.397428], { icon: pin })
.bindPopup('天安门')
.addTo(map);
Watch out
- Copying `iconAnchor` numbers from an example whose icon has different dimensions
- Fixing the anchor but leaving `popupAnchor` unchanged, so the popup covers the pin head
English edition written for Atlaslog from the original Chinese field note. Code is unchanged and runs as-is.
Atlaslog (2026). Pin tip misses the coordinate by 12 pixels. https://www.jingweizhi.com/en/cookbook/icon-anchor.html (accessed 2026-11-03).