{"record":{"id":"23188661ce2f6c32","repo":"Leaflet/Leaflet","slug":"set-map-center-and-zoom-first","errorCode":null,"errorMessage":"Set map center and zoom first.","messagePattern":"Set map center and zoom first\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/map/Map.js","lineNumber":1297,"sourceCode":"\t}\n\n\t_rawPanBy(offset) {\n\t\tDomUtil.setPosition(this._mapPane, this._getMapPanePos().subtract(offset));\n\t}\n\n\t_getZoomSpan() {\n\t\treturn this.getMaxZoom() - this.getMinZoom();\n\t}\n\n\t_panInsideMaxBounds() {\n\t\tif (!this._enforcingBounds) {\n\t\t\tthis.panInsideBounds(this.options.maxBounds);\n\t\t}\n\t}\n\n\t_checkIfLoaded() {\n\t\tif (!this._loaded) {\n\t\t\tthrow new Error('Set map center and zoom first.');\n\t\t}\n\t}\n\n\t// DOM event handling\n\n\t// @section Interaction events\n\t_initEvents(remove) {\n\t\tthis._targets = {};\n\t\tthis._targets[Util.stamp(this._container)] = this;\n\n\t\tconst onOff = remove ? DomEvent.off : DomEvent.on;\n\n\t\t// @event click: PointerEvent\n\t\t// Fired when the user clicks (or taps) the map.\n\t\t// @event dblclick: PointerEvent\n\t\t// Fired when the user double-clicks (or double-taps) the map.\n\t\t// @event pointerdown: PointerEvent\n\t\t// Fired when the user pushes the pointer on the map.","sourceCodeStart":1279,"sourceCodeEnd":1315,"githubUrl":"https://github.com/Leaflet/Leaflet/blob/c96f31a7a350a07cfbc852cf88e6ca69af5f5ec9/src/map/Map.js#L1279-L1315","documentation":"Thrown by Map._checkIfLoaded() (src/map/Map.js:1296) when this._loaded is false. It is called by getCenter() (Map.js:837) and getPixelOrigin() (Map.js:933). _loaded becomes true after the first setView completes and the map fires 'load'; before that, geographic/pixel queries are not meaningful and Leaflet refuses them.","triggerScenarios":"const map = new L.Map('node'); map.getCenter(); before any setView; calling getCenter() synchronously after construction; calling getPixelOrigin() in a beforeAdd hook.","commonSituations":"Calling getCenter()/getPixelOrigin() during component init before setView or fitBounds ran; reading the center in a layer's beforeAdd; SSR or early render where the map never loaded; assuming the map is ready immediately after `new Map`.","solutions":["Call setView(center, zoom) or fitBounds(...) before reading center/pixelOrigin.","Listen for the map's 'load' event and run dependent code in that handler.","Guard: if (map._loaded) { ... } else map.once('load', () => {...}).","For layers, use onAdd (which runs after the map is ready) rather than beforeAdd for center-dependent logic."],"exampleFix":"// before\nconst map = new L.Map('node');\nconst c = map.getCenter(); // _loaded is false -> throws\n// after\nconst map = new L.Map('node').setView([51.5, -0.09], 13);\nconst c = map.getCenter();\n// or defer: map.once('load', () => { const c = map.getCenter(); });","handlingStrategy":"validation","validationCode":"function getCenterIfLoaded(map, fallback) {\n  return map._loaded ? map.getCenter() : fallback;\n}\n// Or subscribe:\nmap.once('load', () => { const c = map.getCenter(); });","typeGuard":"function isMapLoaded(map) { return !!map._loaded; }","tryCatchPattern":"try { return map.getCenter(); }\ncatch (e) { if (/Set map center and zoom first/.test(e.message)) { map.once('load', () => map.getCenter()); } else throw e; }","preventionTips":["Call setView or fitBounds before reading getCenter()/getPixelOrigin().","Run center-dependent code in the map 'load' handler or a layer's onAdd.","Gate reads behind if (map._loaded) ... else map.once('load', ...)."],"tags":["map","lifecycle","not-loaded","order-of-operations"],"backgroundTag":null,"analyzedSha":"c96f31a7a350a07cfbc852cf88e6ca69af5f5ec9","analyzedAt":"2026-08-13T03:13:23.068Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}