{"record":{"id":"88c7cf7020f36159","repo":"angular/components","slug":"cannot-interact-with-a-google-map-transit-layer-be","errorCode":null,"errorMessage":"Cannot interact with a Google Map Transit Layer before it has been initialized. Please wait for the Transit Layer to load before trying to interact with it.","messagePattern":"Cannot interact with a Google Map Transit Layer before it has been initialized\\. Please wait for the Transit Layer to load before trying to interact with it\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/google-maps/map-transit-layer/map-transit-layer.ts","lineNumber":68,"sourceCode":"    }\n  }\n\n  private _initialize(map: google.maps.Map, layerConstructor: typeof google.maps.TransitLayer) {\n    this._zone.runOutsideAngular(() => {\n      this.transitLayer = new layerConstructor();\n      this.transitLayerInitialized.emit(this.transitLayer);\n      this._assertLayerInitialized();\n      this.transitLayer.setMap(map);\n    });\n  }\n\n  ngOnDestroy() {\n    this.transitLayer?.setMap(null);\n  }\n\n  private _assertLayerInitialized(): asserts this is {transitLayer: google.maps.TransitLayer} {\n    if (!this.transitLayer) {\n      throw Error(\n        'Cannot interact with a Google Map Transit Layer before it has been initialized. ' +\n          'Please wait for the Transit Layer to load before trying to interact with it.',\n      );\n    }\n  }\n}\n","sourceCodeStart":50,"sourceCodeEnd":75,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/google-maps/map-transit-layer/map-transit-layer.ts#L50-L75","documentation":"MapTransitLayer throws via _assertLayerInitialized when code interacts with it before the google.maps.TransitLayer instance exists. The native layer is created asynchronously after the Maps JS API loads; any premature interaction hits this guard. The assertion runs unconditionally (also in production).","triggerScenarios":"Any transit-layer API interaction that runs _assertLayerInitialized (from _initialize) when this.transitLayer is still undefined — i.e. before the Maps script loads or before the component finishes initialization.","commonSituations":"Calling layer methods before mapReady; Maps JS API load failure (invalid key, offline, ad-blocker) so the layer is never constructed; interacting from ngOnInit instead of after initialization.","solutions":["Wait for map initialization (mapReady event) before touching the transit layer.","Confirm the Maps JS API script loaded successfully — check console/network for script errors.","Move layer interaction into ngAfterViewInit plus a readiness subscription rather than lifecycle-start hooks.","Optionally null-check the directive's internal state before calling."],"exampleFix":"// before\nngOnInit() {\n  this.transitLayerDirective.someMethod(); // throws\n}\n// after\nthis.map.mapReady.subscribe(() => {\n  this.transitLayerDirective.someMethod();\n});","handlingStrategy":"type-guard","validationCode":"if (layer && (layer as any).transitLayer) { /* safe to interact */ }","typeGuard":"function isTransitLayerInitialized(l: any): l is {transitLayer: google.maps.TransitLayer} {\n  return !!l && !!l.transitLayer;\n}","tryCatchPattern":"try {\n  this.transitLayerDirective.someMethod();\n} catch (e) {\n  if ((e as Error).message.includes('Transit Layer')) {\n    this.map.mapReady.pipe(take(1)).subscribe(() => this.transitLayerDirective.someMethod());\n  } else { throw e; }\n}","preventionTips":["Interact with the transit layer only after the map emits its ready event","Check Maps API script load success (key validity, network, CSP)","Defer template-driven interactions until after initialization","Keep layer setup in a post-init hook rather than lifecycle-start hooks"],"tags":["angular","google-maps","initialization","lifecycle"],"backgroundTag":"component-not-initialized","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}