Atlaslog / Field Notes / MapLibre styles & layers
Field note · MapLibre styles & layers
Custom marker icons 404 and symbols render blank
Quick answer
The symbol layer references icon-image but every icon request returns 404, so markers show nothing or a placeholder square. Causes are a relative path breaking under a sub-route, registration before decode, or a missing @2x asset.
Symptom
- GET /icons/pin.png 404 and GET /icons/pin@2x.png 404 in the network panel.
- Uncaught Error: Could not load image because of 404 Not Found at maplibre-gl.js:4211.
- Images sometimes return 200 yet no icon appears and no error is logged.
Checklist
- Load the image in a plain Image object and print width and height to confirm the URL.
- Check map.hasImage('pin') after registration.
- Verify the page base path, since ./icons/pin.png under /project/detail resolves to /project/icons/pin.png.
- Check pixelRatio and whether an @2x asset exists.
Fix
Use map.loadImage for both 1x and 2x, then map.addImage('pin', image.data, { pixelRatio: 1 }) and pixelRatio 2 before creating the symbol layer, building URLs from an absolute ICON_BASE.
Code
Uncaught Error: Could not load image because of 404 Not Found.
at maplibre-gl.js:4211
GET /icons/pin.png 404 (Not Found)
GET /icons/pin@2x.png 404 (Not Found)var img = new Image();
img.onload = function () { console.log('图片尺寸', img.width, img.height); };
img.onerror = function (e) { console.error('图片加载失败', e); };
img.src = '/icons/pin.png';
console.log('已注册图标', map.hasImage('pin'));
Watch out
- Registering the image after the layer exists does not refresh existing symbols until icon-image is set again.
- Passing an HTMLImageElement that has not finished decoding registers an empty or wrongly sized image.
English edition written for Atlaslog from the original Chinese field note. Code is unchanged and runs as-is.
Atlaslog (2026). Custom marker icons 404 and symbols render blank. https://www.jingweizhi.com/en/cookbook/maplibre-icon-404.html (accessed 2026-11-03).