/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
// We need this to ensure that the type reference is present in the generated .d.ts
// that we publish to npm. Internally this is removed during import.
///
import {
Component,
ElementRef,
Input,
OnChanges,
OnDestroy,
OnInit,
Output,
ViewEncapsulation,
PLATFORM_ID,
NgZone,
SimpleChanges,
EventEmitter,
inject,
} from '@angular/core';
import {isPlatformBrowser} from '@angular/common';
import {Observable} from 'rxjs';
import {MapEventManager} from '../map-event-manager';
import {take} from 'rxjs/operators';
interface GoogleMapsWindow extends Window {
gm_authFailure?: () => void;
google?: typeof google;
}
/** default options set to the Googleplex */
export const DEFAULT_OPTIONS: google.maps.MapOptions = {
center: {lat: 37.421995, lng: -122.084092},
zoom: 17,
// Note: the type conversion here isn't necessary for our CI, but it resolves a g3 failure.
mapTypeId: 'roadmap' as unknown as google.maps.MapTypeId,
};
/** Arbitrary default height for the map element */
export const DEFAULT_HEIGHT = '500px';
/** Arbitrary default width for the map element */
export const DEFAULT_WIDTH = '500px';
/**
* Angular component that renders a Google Map via the Google Maps JavaScript
* API.
* @see https://developers.google.com/maps/documentation/javascript/reference/
*/
@Component({
selector: 'google-map',
exportAs: 'googleMap',
template: '
',
encapsulation: ViewEncapsulation.None,
})
export class GoogleMap implements OnChanges, OnInit, OnDestroy {
private readonly _elementRef = inject(ElementRef);
private _ngZone = inject(NgZone);
private _eventManager = new MapEventManager(inject(NgZone));
private _mapEl!: HTMLElement;
private _existingAuthFailureCallback!: GoogleMapsWindow['gm_authFailure'];
/**
* The underlying google.maps.Map object
*
* See developers.google.com/maps/documentation/javascript/reference/map#Map
*/
googleMap?: google.maps.Map;
/** Whether we're currently rendering inside a browser. */
_isBrowser: boolean;
/** Height of the map. Set this to `null` if you'd like to control the height through CSS. */
@Input() height: string | number | null = DEFAULT_HEIGHT;
/** Width of the map. Set this to `null` if you'd like to control the width through CSS. */
@Input() width: string | number | null = DEFAULT_WIDTH;
/**
* The Map ID of the map. This parameter cannot be set or changed after a map is instantiated.
* See: https://developers.google.com/maps/documentation/javascript/reference/map#MapOptions.mapId
*/
@Input() mapId: string | undefined;
/**
* Type of map that should be rendered. E.g. hybrid map, terrain map etc.
* See: https://developers.google.com/maps/documentation/javascript/reference/map#MapTypeId
*/
@Input() mapTypeId: google.maps.MapTypeId | undefined;
@Input()
set center(center: google.maps.LatLngLiteral | google.maps.LatLng) {
this._center = center;
}
private _center!: google.maps.LatLngLiteral | google.maps.LatLng;
@Input()
set zoom(zoom: number) {
this._zoom = zoom;
}
private _zoom!: number;
@Input()
set options(options: google.maps.MapOptions) {
this._options = options || DEFAULT_OPTIONS;
}
private _options = DEFAULT_OPTIONS;
/** Event emitted when the map is initialized. */
@Output() readonly mapInitialized: EventEmitter =
new EventEmitter();
/**
* See
* https://developers.google.com/maps/documentation/javascript/events#auth-errors
*/
@Output() readonly authFailure: EventEmitter = new EventEmitter();
/**
* See
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.bounds_changed
*/
@Output() readonly boundsChanged: Observable =
this._eventManager.getLazyEmitter('bounds_changed');
/**
* See
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.center_changed
*/
@Output() readonly centerChanged: Observable =
this._eventManager.getLazyEmitter('center_changed');
/**
* See
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.click
*/
@Output() readonly mapClick: Observable =
this._eventManager.getLazyEmitter(
'click',
);
/**
* See
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.dblclick
*/
@Output() readonly mapDblclick: Observable =
this._eventManager.getLazyEmitter('dblclick');
/**
* See
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.drag
*/
@Output() readonly mapDrag: Observable = this._eventManager.getLazyEmitter('drag');
/**
* See
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.dragend
*/
@Output() readonly mapDragend: Observable =
this._eventManager.getLazyEmitter('dragend');
/**
* See
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.dragstart
*/
@Output() readonly mapDragstart: Observable =
this._eventManager.getLazyEmitter('dragstart');
/**
* See
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.heading_changed
*/
@Output() readonly headingChanged: Observable =
this._eventManager.getLazyEmitter('heading_changed');
/**
* See
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.idle
*/
@Output() readonly idle: Observable = this._eventManager.getLazyEmitter('idle');
/**
* See
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.maptypeid_changed
*/
@Output() readonly maptypeidChanged: Observable =
this._eventManager.getLazyEmitter('maptypeid_changed');
/**
* See
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.mousemove
*/
@Output()
readonly mapMousemove: Observable =
this._eventManager.getLazyEmitter('mousemove');
/**
* See
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.mouseout
*/
@Output() readonly mapMouseout: Observable =
this._eventManager.getLazyEmitter('mouseout');
/**
* See
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.mouseover
*/
@Output() readonly mapMouseover: Observable =
this._eventManager.getLazyEmitter('mouseover');
/**
* See
* developers.google.com/maps/documentation/javascript/reference/map#Map.projection_changed
*/
@Output() readonly projectionChanged: Observable =
this._eventManager.getLazyEmitter('projection_changed');
/**
* See
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.rightclick
*/
@Output() readonly mapRightclick: Observable =
this._eventManager.getLazyEmitter('rightclick');
/**
* See
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.tilesloaded
*/
@Output() readonly tilesloaded: Observable =
this._eventManager.getLazyEmitter('tilesloaded');
/**
* See
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.tilt_changed
*/
@Output() readonly tiltChanged: Observable =
this._eventManager.getLazyEmitter('tilt_changed');
/**
* See
* https://developers.google.com/maps/documentation/javascript/reference/map#Map.zoom_changed
*/
@Output() readonly zoomChanged: Observable =
this._eventManager.getLazyEmitter('zoom_changed');
constructor() {
const platformId = inject