Skip to content

Commit d99167e

Browse files
committed
fix(google-maps): make info window open method compatible with advanced marker
Currently the info window has two methods: one for any `MapAnchorPoint` and one for an advanced marker. These changes deprecate the latter and make the former compatible with advanced markers.
1 parent 7a085c6 commit d99167e

File tree

10 files changed

+75
-45
lines changed

10 files changed

+75
-45
lines changed

src/dev-app/google-map/google-map-demo.html

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,39 @@
88
(mapClick)="handleClick($event)"
99
(mapMousemove)="handleMove($event)"
1010
(mapRightclick)="handleRightclick()"
11-
[mapTypeId]="mapTypeId">
11+
[mapTypeId]="mapTypeId"
12+
[mapId]="mapId">
1213
<map-marker-clusterer [imagePath]="markerClustererImagePath">
1314
<map-marker #firstMarker="mapMarker"
1415
[position]="center"
15-
(mapClick)="clickMarker(firstMarker)"></map-marker>
16+
(mapClick)="infoWindow.open(firstMarker)"></map-marker>
1617
@for (markerPosition of markerPositions; track markerPosition) {
1718
<map-marker #marker="mapMarker"
1819
[position]="markerPosition"
1920
[options]="markerOptions"
20-
(mapClick)="clickMarker(marker)"></map-marker>
21+
(mapClick)="infoWindow.open(marker)"></map-marker>
2122
}
2223
</map-marker-clusterer>
2324
@if (hasAdvancedMarker) {
2425
<map-advanced-marker
2526
#secondMarker="mapAdvancedMarker"
26-
(mapClick)="clickAdvancedMarker(secondMarker)"
27+
(mapClick)="infoWindow.open(secondMarker)"
2728
title="Advanced Marker"
2829
[gmpDraggable]="false"
29-
[content]="advancedMarkerContent"
30+
[content]="hasAdvancedMarkerCustomContent ? advancedMarkerContent : null"
3031
[position]="mapAdvancedMarkerPosition">
3132

3233
<svg #advancedMarkerContent fill="oklch(69.02% .277 332.77)" viewBox="0 0 960 960" width="50px" height="50px" xml:space="preserve">
3334
<g>
34-
<polygon points="562.6,109.8 804.1,629.5 829.2,233.1 "/>
35-
<polygon points="624.9,655.9 334.3,655.9 297.2,745.8 479.6,849.8 662,745.8 "/>
36-
<polygon points="384.1,539.3 575.2,539.3 479.6,307 "/>
37-
<polygon points="396.6,109.8 130,233.1 155.1,629.5 "/>
35+
<polygon points="562.6,109.8 804.1,629.5 829.2,233.1"/>
36+
<polygon points="624.9,655.9 334.3,655.9 297.2,745.8 479.6,849.8 662,745.8"/>
37+
<polygon points="384.1,539.3 575.2,539.3 479.6,307"/>
38+
<polygon points="396.6,109.8 130,233.1 155.1,629.5"/>
3839
</g>
3940
</svg>
4041
</map-advanced-marker>
4142
}
42-
<map-info-window>Testing 1 2 3</map-info-window>
43+
<map-info-window #infoWindow="mapInfoWindow">Testing 1 2 3</map-info-window>
4344
@if (isPolylineDisplayed) {
4445
<map-polyline [options]="polylineOptions"></map-polyline>
4546
}
@@ -213,6 +214,13 @@
213214
</label>
214215
</div>
215216

217+
<div>
218+
<label>
219+
Toggle custom content for Advanced Marker
220+
<input type="checkbox" (click)="toggleAdvancedMarkerCustomContent()">
221+
</label>
222+
</div>
223+
216224
<div>
217225
<button mat-button (click)="calculateDirections()">
218226
Calculate directions between first two markers

src/dev-app/google-map/google-map-demo.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ let apiLoadingPromise: Promise<unknown> | null = null;
8282
],
8383
})
8484
export class GoogleMapDemo {
85-
@ViewChild(MapInfoWindow) infoWindow: MapInfoWindow;
8685
@ViewChild(MapPolyline) polyline: MapPolyline;
8786
@ViewChild(MapPolygon) polygon: MapPolygon;
8887
@ViewChild(MapRectangle) rectangle: MapRectangle;
@@ -146,6 +145,9 @@ export class GoogleMapDemo {
146145
isTransitLayerDisplayed = false;
147146
isBicyclingLayerDisplayed = false;
148147
hasAdvancedMarker = false;
148+
hasAdvancedMarkerCustomContent = true;
149+
// This is necessary for testing advanced markers. It seems like any value works locally.
150+
mapId = '123';
149151

150152
mapTypeId: google.maps.MapTypeId;
151153
mapTypeIds = ['hybrid', 'roadmap', 'satellite', 'terrain'] as google.maps.MapTypeId[];
@@ -173,17 +175,6 @@ export class GoogleMapDemo {
173175
this.display = event.latLng?.toJSON();
174176
}
175177

176-
clickMarker(marker: MapMarker) {
177-
this.infoWindow.open(marker);
178-
}
179-
180-
clickAdvancedMarker(advancedMarker: MapAdvancedMarker) {
181-
this.infoWindow.openAdvancedMarkerElement(
182-
advancedMarker.advancedMarker,
183-
advancedMarker.advancedMarker.title,
184-
);
185-
}
186-
187178
handleRightclick() {
188179
this.markerPositions.pop();
189180
}
@@ -269,6 +260,10 @@ export class GoogleMapDemo {
269260
this.hasAdvancedMarker = !this.hasAdvancedMarker;
270261
}
271262

263+
toggleAdvancedMarkerCustomContent() {
264+
this.hasAdvancedMarkerCustomContent = !this.hasAdvancedMarkerCustomContent;
265+
}
266+
272267
calculateDirections() {
273268
if (this.markerPositions.length >= 2) {
274269
const request: google.maps.DirectionsRequest = {

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
import {GoogleMap} from '../google-map/google-map';
2626
import {MapEventManager} from '../map-event-manager';
2727
import {Observable} from 'rxjs';
28+
import {MapAnchorPoint} from '../map-anchor-point';
2829

2930
/**
3031
* Default options for the Google Maps marker component. Displays a marker
@@ -44,7 +45,7 @@ export const DEFAULT_MARKER_OPTIONS = {
4445
exportAs: 'mapAdvancedMarker',
4546
standalone: true,
4647
})
47-
export class MapAdvancedMarker implements OnInit, OnChanges, OnDestroy {
48+
export class MapAdvancedMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint {
4849
private _eventManager = new MapEventManager(inject(NgZone));
4950

5051
/**
@@ -80,10 +81,10 @@ export class MapAdvancedMarker implements OnInit, OnChanges, OnDestroy {
8081
* See: https://developers.google.com/maps/documentation/javascript/reference/advanced-markers#AdvancedMarkerElementOptions.content
8182
*/
8283
@Input()
83-
set content(content: Node | google.maps.marker.PinElement) {
84+
set content(content: Node | google.maps.marker.PinElement | null) {
8485
this._content = content;
8586
}
86-
private _content: Node;
87+
private _content: Node | null;
8788

8889
/**
8990
* If true, the AdvancedMarkerElement can be dragged.
@@ -231,6 +232,11 @@ export class MapAdvancedMarker implements OnInit, OnChanges, OnDestroy {
231232
}
232233
}
233234

235+
getAnchor(): google.maps.marker.AdvancedMarkerElement {
236+
this._assertInitialized();
237+
return this.advancedMarker;
238+
}
239+
234240
/** Creates a combined options object using the passed-in options and the individual inputs. */
235241
private _combineOptions(): google.maps.marker.AdvancedMarkerElementOptions {
236242
const options = this._options || DEFAULT_MARKER_OPTIONS;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
/// <reference types="google.maps" />
1111

1212
export interface MapAnchorPoint {
13-
getAnchor(): google.maps.MVCObject;
13+
getAnchor(): google.maps.MVCObject | google.maps.marker.AdvancedMarkerElement;
1414
}

src/google-maps/map-info-window/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,17 @@ export class GoogleMapDemo {
3636

3737
```html
3838
<!-- google-maps-demo.component.html -->
39-
<google-map
39+
<google-map
4040
height="400px"
4141
width="750px"
4242
[center]="center"
4343
[zoom]="zoom"
4444
(mapClick)="addMarker($event)">
4545
@for (position of markerPositions; track position) {
46-
<map-marker #marker="mapMarker" [position]="position" (mapClick)="openInfoWindow(marker)" />
46+
<map-advanced-marker
47+
#marker="mapMarker"
48+
[position]="position"
49+
(mapClick)="openInfoWindow(marker)" />
4750
}
4851
<map-info-window>Info Window content</map-info-window>
4952
</google-map>

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -195,29 +195,36 @@ export class MapInfoWindow implements OnInit, OnDestroy {
195195

196196
/**
197197
* Opens the MapInfoWindow using the provided AdvancedMarkerElement.
198+
* @deprecated Use the `open` method instead.
199+
* @breaking-change 20.0.0
198200
*/
199201
openAdvancedMarkerElement(
200202
advancedMarkerElement: google.maps.marker.AdvancedMarkerElement,
201203
content?: string | Element | Text,
202204
): void {
203-
this._assertInitialized();
204-
if (!advancedMarkerElement) {
205-
return;
206-
}
207-
208-
this.infoWindow.close();
209-
if (content) {
210-
this.infoWindow.setContent(content);
211-
}
212-
this.infoWindow.open(this._googleMap.googleMap, advancedMarkerElement);
205+
this.open(
206+
{
207+
getAnchor: () => advancedMarkerElement,
208+
},
209+
undefined,
210+
content,
211+
);
213212
}
214213

215214
/**
216215
* Opens the MapInfoWindow using the provided anchor. If the anchor is not set,
217216
* then the position property of the options input is used instead.
218217
*/
219-
open(anchor?: MapAnchorPoint, shouldFocus?: boolean) {
218+
open(anchor?: MapAnchorPoint, shouldFocus?: boolean, content?: string | Element | Text): void {
220219
this._assertInitialized();
220+
221+
if ((typeof ngDevMode === 'undefined' || ngDevMode) && anchor && !anchor.getAnchor) {
222+
throw new Error(
223+
'Specified anchor does not implement the `getAnchor` method. ' +
224+
'It cannot be used to open an info window.',
225+
);
226+
}
227+
221228
const anchorObject = anchor ? anchor.getAnchor() : undefined;
222229

223230
// Prevent the info window from initializing when trying to reopen on the same anchor.
@@ -226,6 +233,9 @@ export class MapInfoWindow implements OnInit, OnDestroy {
226233
// case where the window doesn't have an anchor, but is placed at a particular position.
227234
if (this.infoWindow.get('anchor') !== anchorObject || !anchorObject) {
228235
this._elementRef.nativeElement.style.display = '';
236+
if (content) {
237+
this.infoWindow.setContent(content);
238+
}
229239
this.infoWindow.open({
230240
map: this._googleMap.googleMap,
231241
anchor: anchorObject,

src/google-maps/map-marker/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
The `MapMarker` component wraps the [`google.maps.Marker` class](https://developers.google.com/maps/documentation/javascript/reference/marker#Marker) from the Google Maps JavaScript API. The `MapMarker` component displays a marker on the map when it is a content child of a `GoogleMap` component. Like `GoogleMap`, this component offers an `options` input as well as convenience inputs for `position`, `title`, `label`, and `clickable`, and supports all `google.maps.Marker` events as outputs.
44

5+
**Note:** As of 2024, Google Maps has deprecated the `Marker` class. Consider using the `map-advanced-marker` instead.
6+
57
## Example
68

79
```typescript
@@ -29,7 +31,7 @@ export class GoogleMapDemo {
2931

3032
```html
3133
<!-- google-map-demo.component.html -->
32-
<google-map
34+
<google-map
3335
height="400px"
3436
width="750px"
3537
[center]="center"

src/universal-app/kitchen-sink/kitchen-sink.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,9 @@ <h2>YouTube player</h2>
585585

586586
<h2>Google Map</h2>
587587
<!-- position: relative prevents the "Map failed to load" element from leaving the container -->
588-
<google-map height="400px" width="750px" style="position: relative">
588+
<google-map height="400px" width="750px" mapId="123" style="position: relative">
589589
<map-marker [position]="{lat: 24, lng: 12}"></map-marker>
590+
<map-advanced-marker [position]="{lat: 12, lng: 12}"></map-advanced-marker>
590591
<map-info-window>Hello</map-info-window>
591592
<map-polyline
592593
[options]="{

src/universal-app/kitchen-sink/kitchen-sink.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
MapRectangle,
2323
MapTrafficLayer,
2424
MapTransitLayer,
25+
MapAdvancedMarker,
2526
} from '@angular/google-maps';
2627
import {MatAutocompleteModule} from '@angular/material/autocomplete';
2728
import {MatBadgeModule} from '@angular/material/badge';
@@ -196,6 +197,7 @@ export class TestEntryComponent {}
196197
MapInfoWindow,
197198
MapKmlLayer,
198199
MapMarker,
200+
MapAdvancedMarker,
199201
MapMarkerClusterer,
200202
MapPolygon,
201203
MapPolyline,

tools/public_api_guard/google-maps/google-maps.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,12 @@ export class GoogleMapsModule {
135135
export type HeatmapData = google.maps.MVCArray<google.maps.LatLng | google.maps.visualization.WeightedLocation | google.maps.LatLngLiteral> | (google.maps.LatLng | google.maps.visualization.WeightedLocation | google.maps.LatLngLiteral)[];
136136

137137
// @public
138-
export class MapAdvancedMarker implements OnInit, OnChanges, OnDestroy {
138+
export class MapAdvancedMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint {
139139
constructor(_googleMap: GoogleMap, _ngZone: NgZone);
140140
advancedMarker: google.maps.marker.AdvancedMarkerElement;
141-
set content(content: Node | google.maps.marker.PinElement);
141+
set content(content: Node | google.maps.marker.PinElement | null);
142+
// (undocumented)
143+
getAnchor(): google.maps.marker.AdvancedMarkerElement;
142144
set gmpDraggable(draggable: boolean);
143145
readonly mapClick: Observable<google.maps.MapMouseEvent>;
144146
readonly mapDrag: Observable<google.maps.MapMouseEvent>;
@@ -164,7 +166,7 @@ export class MapAdvancedMarker implements OnInit, OnChanges, OnDestroy {
164166
// @public
165167
export interface MapAnchorPoint {
166168
// (undocumented)
167-
getAnchor(): google.maps.MVCObject;
169+
getAnchor(): google.maps.MVCObject | google.maps.marker.AdvancedMarkerElement;
168170
}
169171

170172
// @public (undocumented)
@@ -391,7 +393,8 @@ export class MapInfoWindow implements OnInit, OnDestroy {
391393
ngOnDestroy(): void;
392394
// (undocumented)
393395
ngOnInit(): void;
394-
open(anchor?: MapAnchorPoint, shouldFocus?: boolean): void;
396+
open(anchor?: MapAnchorPoint, shouldFocus?: boolean, content?: string | Element | Text): void;
397+
// @deprecated
395398
openAdvancedMarkerElement(advancedMarkerElement: google.maps.marker.AdvancedMarkerElement, content?: string | Element | Text): void;
396399
// (undocumented)
397400
set options(options: google.maps.InfoWindowOptions);

0 commit comments

Comments
 (0)