angular/components · error

Cannot access Google Map information before the API has been

Error message

Cannot access Google Map information before the API has been initialized. Please wait for the API to load before trying to interact with it.

What it means

GoogleMap resolves the underlying google.maps.Map asynchronously after the API loads. Methods like fitBounds, panTo, getBounds, getCenter all call _assertInitialized, which throws if this.googleMap is still unset. It prevents calling Maps API methods on a not-yet-created map object.

Source

Thrown at src/google-maps/google-map/google-map.ts:533

  private _combineOptions(): google.maps.MapOptions {
    const options = this._options || {};
    return {
      ...options,
      // It's important that we set **some** kind of `center` and `zoom`, otherwise
      // Google Maps will render a blank rectangle which looks broken.
      center: this._center || options.center || DEFAULT_OPTIONS.center,
      zoom: this._zoom ?? options.zoom ?? DEFAULT_OPTIONS.zoom,
      // Passing in an undefined `mapTypeId` seems to break tile loading
      // so make sure that we have some kind of default (see #22082).
      mapTypeId: this.mapTypeId || options.mapTypeId || DEFAULT_OPTIONS.mapTypeId,
      mapId: this.mapId || options.mapId,
    };
  }

  /** Asserts that the map has been initialized. */
  private _assertInitialized(): asserts this is {googleMap: google.maps.Map} {
    if (!this.googleMap && (typeof ngDevMode === 'undefined' || ngDevMode)) {
      throw Error(
        'Cannot access Google Map information before the API has been initialized. ' +
          'Please wait for the API to load before trying to interact with it.',
      );
    }
  }
}

const cssUnitsPattern = /([A-Za-z%]+)$/;

/** Coerces a value to a CSS pixel value. */
function coerceCssPixelValue(value: any): string {
  if (value == null) {
    return '';
  }

  return cssUnitsPattern.test(value) ? value : `${value}px`;
}

View on GitHub (pinned to 0411926e7d)

Solutions

  1. Await the map instance via the component's exposed promise (e.g. _resolveMap()) before calling methods.
  2. Queue viewport operations inside the promise callback.
  3. Subscribe to map events (e.g. bounds_changed / initialization) before interacting.
  4. In tests, await test harness stability or the resolve promise.

Example fix

// before
const center = this.map.getCenter();
// after
this.map._resolveMap().then(() => {
  const center = this.map.getCenter();
});
Defensive patterns

Strategy: try-catch

Validate before calling

async function whenMapReady(map: any) {
  await map._resolveMap();
  return map;
}
// usage: const m = await whenMapReady(mapComponent); m.fitBounds(bounds);

Type guard

function isMapReady(c: any): c is {googleMap: google.maps.Map} {
  return c?.googleMap != null;
}

Try / catch

try {
  this.map.panTo(latLng);
} catch (e) {
  if (String((e as Error).message).includes('before the API has been initialized')) {
    this.map._resolveMap().then(() => this.map.panTo(latLng));
  }
}

Prevention

When it happens

Trigger: Calling fitBounds, panBy, panTo, panToBounds, getBounds, or getCenter on a <google-map> reference before the async map initialization completed (e.g. synchronously in the parent's ngAfterViewInit).

Common situations: Adjusting the viewport right after creating the map in a component; running map queries in unit tests without waiting; slow network delaying the Maps script so user code races the map creation.

Related errors


AI-assisted analysis of angular/components@0411926e7d (2026-08-31). Data as JSON: /api/errors/80dd44c4a12e62c3. Report an issue: GitHub.