Skip to content

Commit d0ab041

Browse files
authored
fix(google-maps): allow different anchor objects for info window (#19378)
Currently the info window is limited only to markers, but the API allows for any kind of `MVCObject`. These changes expand the type so that we can make it more flexible in the future.
1 parent 95007e5 commit d0ab041

File tree

6 files changed

+38
-9
lines changed

6 files changed

+38
-9
lines changed

src/google-maps/map-anchor-point.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
// Workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1265
10+
/// <reference types="googlemaps" />
11+
12+
export interface MapAnchorPoint {
13+
getAnchor(): google.maps.MVCObject;
14+
}

src/google-maps/map-info-window/map-info-window.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ describe('MapInfoWindow', () => {
110110

111111
it('exposes methods that change the configuration of the info window', () => {
112112
const fakeMarker = {} as unknown as google.maps.Marker;
113-
const fakeMarkerComponent = {marker: fakeMarker} as unknown as MapMarker;
113+
const fakeMarkerComponent = {
114+
marker: fakeMarker,
115+
getAnchor: () => fakeMarker
116+
} as unknown as MapMarker;
114117
const infoWindowSpy = createInfoWindowSpy({});
115118
createInfoWindowConstructorSpy(infoWindowSpy).and.callThrough();
116119

src/google-maps/map-info-window/map-info-window.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {map, take, takeUntil} from 'rxjs/operators';
2323

2424
import {GoogleMap} from '../google-map/google-map';
2525
import {MapEventManager} from '../map-event-manager';
26-
import {MapMarker} from '../map-marker/map-marker';
26+
import {MapAnchorPoint} from '../map-anchor-point';
2727

2828
/**
2929
* Angular component that renders a Google Maps info window via the Google Maps JavaScript API.
@@ -167,14 +167,13 @@ export class MapInfoWindow implements OnInit, OnDestroy {
167167
}
168168

169169
/**
170-
* Opens the MapInfoWindow using the provided MapMarker as the anchor. If the anchor is not set,
170+
* Opens the MapInfoWindow using the provided anchor. If the anchor is not set,
171171
* then the position property of the options input is used instead.
172172
*/
173-
open(anchor?: MapMarker) {
173+
open(anchor?: MapAnchorPoint) {
174174
this._assertInitialized();
175-
const marker = anchor ? anchor.marker : undefined;
176175
this._elementRef.nativeElement.style.display = '';
177-
this.infoWindow.open(this._googleMap.googleMap, marker);
176+
this.infoWindow.open(this._googleMap.googleMap, anchor ? anchor.getAnchor() : undefined);
178177
}
179178

180179
private _combineOptions(): Observable<google.maps.InfoWindowOptions> {

src/google-maps/map-marker/map-marker.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {map, take, takeUntil} from 'rxjs/operators';
2424

2525
import {GoogleMap} from '../google-map/google-map';
2626
import {MapEventManager} from '../map-event-manager';
27+
import {MapAnchorPoint} from '../map-anchor-point';
2728

2829
/**
2930
* Default options for the Google Maps marker component. Displays a marker
@@ -44,7 +45,7 @@ export const DEFAULT_MARKER_OPTIONS = {
4445
changeDetection: ChangeDetectionStrategy.OnPush,
4546
encapsulation: ViewEncapsulation.None,
4647
})
47-
export class MapMarker implements OnInit, OnDestroy {
48+
export class MapMarker implements OnInit, OnDestroy, MapAnchorPoint {
4849
private _eventManager = new MapEventManager(this._ngZone);
4950
private readonly _options =
5051
new BehaviorSubject<google.maps.MarkerOptions>(DEFAULT_MARKER_OPTIONS);
@@ -384,6 +385,12 @@ export class MapMarker implements OnInit, OnDestroy {
384385
return this.marker.getZIndex() || null;
385386
}
386387

388+
/** Gets the anchor point that can be used to attach other Google Maps objects. */
389+
getAnchor(): google.maps.MVCObject {
390+
this._assertInitialized();
391+
return this.marker;
392+
}
393+
387394
private _combineOptions(): Observable<google.maps.MarkerOptions> {
388395
return combineLatest([this._options, this._title, this._position, this._label, this._clickable])
389396
.pipe(map(([options, title, position, label, clickable]) => {

src/google-maps/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ export {MapMarker} from './map-marker/map-marker';
1515
export {MapPolygon} from './map-polygon/map-polygon';
1616
export {MapPolyline} from './map-polyline/map-polyline';
1717
export {MapRectangle} from './map-rectangle/map-rectangle';
18+
export {MapAnchorPoint} from './map-anchor-point';

tools/public_api_guard/google-maps/google-maps.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export declare class GoogleMapsModule {
5656
static ɵmod: i0.ɵɵNgModuleDefWithMeta<GoogleMapsModule, [typeof i1.GoogleMap, typeof i2.MapCircle, typeof i3.MapGroundOverlay, typeof i4.MapInfoWindow, typeof i5.MapMarker, typeof i6.MapPolygon, typeof i7.MapPolyline, typeof i8.MapRectangle], never, [typeof i1.GoogleMap, typeof i2.MapCircle, typeof i3.MapGroundOverlay, typeof i4.MapInfoWindow, typeof i5.MapMarker, typeof i6.MapPolygon, typeof i7.MapPolyline, typeof i8.MapRectangle]>;
5757
}
5858

59+
export interface MapAnchorPoint {
60+
getAnchor(): google.maps.MVCObject;
61+
}
62+
5963
export declare class MapCircle implements OnInit, OnDestroy {
6064
set center(center: google.maps.LatLng | google.maps.LatLngLiteral);
6165
centerChanged: Observable<void>;
@@ -121,12 +125,12 @@ export declare class MapInfoWindow implements OnInit, OnDestroy {
121125
getZIndex(): number;
122126
ngOnDestroy(): void;
123127
ngOnInit(): void;
124-
open(anchor?: MapMarker): void;
128+
open(anchor?: MapAnchorPoint): void;
125129
static ɵdir: i0.ɵɵDirectiveDefWithMeta<MapInfoWindow, "map-info-window", never, { "options": "options"; "position": "position"; }, { "closeclick": "closeclick"; "contentChanged": "contentChanged"; "domready": "domready"; "positionChanged": "positionChanged"; "zindexChanged": "zindexChanged"; }, never>;
126130
static ɵfac: i0.ɵɵFactoryDef<MapInfoWindow, never>;
127131
}
128132

129-
export declare class MapMarker implements OnInit, OnDestroy {
133+
export declare class MapMarker implements OnInit, OnDestroy, MapAnchorPoint {
130134
animationChanged: Observable<void>;
131135
set clickable(clickable: boolean);
132136
clickableChanged: Observable<void>;
@@ -155,6 +159,7 @@ export declare class MapMarker implements OnInit, OnDestroy {
155159
visibleChanged: Observable<void>;
156160
zindexChanged: Observable<void>;
157161
constructor(_googleMap: GoogleMap, _ngZone: NgZone);
162+
getAnchor(): google.maps.MVCObject;
158163
getAnimation(): google.maps.Animation | null;
159164
getClickable(): boolean;
160165
getCursor(): string | null;

0 commit comments

Comments
 (0)