Atlaslog
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.

Last verified 2026-11-03 · Multiple versions · Cite this page
Applies to: Leaflet 1.9.x / Mapbox GL JS 2.15
Symptom
Checklist
  1. Measure the icon's intrinsic size and the tip's pixel position inside it
  2. Check whether `L.icon` specifies `iconAnchor`
  3. Place a 1x1 test marker at the same coordinate and compare it with the pin tip
  4. Check CSS transforms or margins applied to `.leaflet-marker-icon`
  5. 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
All 120 field notesChinese originalRunnable examples
English edition written for Atlaslog from the original Chinese field note. Code is unchanged and runs as-is.
Cite this page

Atlaslog (2026). Pin tip misses the coordinate by 12 pixels. https://www.jingweizhi.com/en/cookbook/icon-anchor.html (accessed 2026-11-03).