{"record":{"id":"1ebf1b7812f158c1","repo":"angular/components","slug":"cannot-interact-with-a-google-map-kmllayer-before","errorCode":null,"errorMessage":"Cannot interact with a Google Map KmlLayer before it has been initialized. Please wait for the KmlLayer to load before trying to interact with it.","messagePattern":"Cannot interact with a Google Map KmlLayer before it has been initialized\\. Please wait for the KmlLayer to load before trying to interact with it\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/google-maps/map-kml-layer/map-kml-layer.ts","lineNumber":204,"sourceCode":"        this._assertInitialized();\n        this.kmlLayer.setOptions(options);\n      }\n    });\n  }\n\n  private _watchForUrlChanges() {\n    this._url.pipe(takeUntil(this._destroyed)).subscribe(url => {\n      if (url && this.kmlLayer) {\n        this._assertInitialized();\n        this.kmlLayer.setUrl(url);\n      }\n    });\n  }\n\n  private _assertInitialized(): asserts this is {kmlLayer: google.maps.KmlLayer} {\n    if (typeof ngDevMode === 'undefined' || ngDevMode) {\n      if (!this.kmlLayer) {\n        throw Error(\n          'Cannot interact with a Google Map KmlLayer before it has been ' +\n            'initialized. Please wait for the KmlLayer to load before trying to interact with it.',\n        );\n      }\n    }\n  }\n}\n","sourceCodeStart":186,"sourceCodeEnd":212,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/google-maps/map-kml-layer/map-kml-layer.ts#L186-L212","documentation":"Thrown by MapKmlLayer's dev-mode assertion when its internal google.maps.KmlLayer is still undefined. The layer is constructed asynchronously after the map and layer inputs are ready. The guard ensures early interaction produces an actionable error instead of an undefined-object TypeError.","triggerScenarios":"Calling getDefaultViewport(), getMetadata(), getStatus(), getUrl(), getZIndex(), or initializing paths that run before kmlLayer is created — e.g. calling these in ngOnInit, before the map is ready, or before the Google Maps API loads.","commonSituations":"Reading KML layer status right after mounting, polling getStatus before initialization, conditional rendering (*ngIf) that recreates the layer while old references are used, missing API key/script so initialization never completes.","solutions":["Defer accessor calls until after view init or map ready (MapComponent.ready subscription).","Poll/await layer status only after initialization completes; use ngAfterViewInit or a ready flag.","Verify the Google Maps script loads successfully (check network/console for script errors).","Ensure the KML layer component is inside a MapComponent so it receives the map and initializes.","Wrap accessor calls in try-catch as a dev-time safety net."],"exampleFix":"// before\nconst status = this.kmlLayer.getStatus(); // throws too early\n// after\nthis.map.ready.subscribe(() => {\n  const status = this.kmlLayer.getStatus();\n});","handlingStrategy":"validation","validationCode":"if (!kmlLayerRef) { /* defer status polling */ }","typeGuard":"function isKmlLayerReady(l: MapKmlLayer | undefined): l is MapKmlLayer { return !!l; }","tryCatchPattern":"try {\n  const status = kmlLayer.getStatus();\n} catch (e) {\n  if ((e as Error).message.includes('KmlLayer')) {\n    // retry after map ready\n  } else { throw e; }\n}","preventionTips":["Start status polling only after ngAfterViewInit/map ready.","Keep layer inside the map component template.","Verify API script loads before mounting layers.","Don't poll in ngOnInit.","Guard stale references after *ngIf re-creation."],"tags":["angular","google-maps","lifecycle","initialization"],"backgroundTag":"component-accessed-before-initialization","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}