Skip to content

Commit 10ecfa0

Browse files
authored
feat(google-maps): add icon input to marker (#22357)
Currently the only way to control the icon of a marker is through the `options` object which can be cumbersome. These changes add a dedicated `icon` input which can be used to only change the icon. Fixes #22097.
1 parent 5384550 commit 10ecfa0

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ describe('MapMarker', () => {
4545
title: undefined,
4646
label: undefined,
4747
clickable: undefined,
48+
icon: undefined,
4849
map: mapSpy,
4950
});
5051
});
@@ -55,6 +56,7 @@ describe('MapMarker', () => {
5556
title: 'marker title',
5657
label: 'marker label',
5758
clickable: false,
59+
icon: 'icon.png',
5860
map: mapSpy,
5961
};
6062
const markerSpy = createMarkerSpy(options);
@@ -65,6 +67,7 @@ describe('MapMarker', () => {
6567
fixture.componentInstance.title = options.title;
6668
fixture.componentInstance.label = options.label;
6769
fixture.componentInstance.clickable = options.clickable;
70+
fixture.componentInstance.icon = 'icon.png';
6871
fixture.detectChanges();
6972

7073
expect(markerConstructorSpy).toHaveBeenCalledWith(options);
@@ -221,6 +224,7 @@ describe('MapMarker', () => {
221224
[label]="label"
222225
[clickable]="clickable"
223226
[options]="options"
227+
[icon]="icon"
224228
(mapClick)="handleClick()"
225229
(positionChanged)="handlePositionChanged()">
226230
</map-marker>
@@ -233,6 +237,7 @@ class TestApp {
233237
label?: string|google.maps.MarkerLabel;
234238
clickable?: boolean;
235239
options?: google.maps.MarkerOptions;
240+
icon?: string;
236241

237242
handleClick() {}
238243

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ export class MapMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint {
9595
}
9696
private _options: google.maps.MarkerOptions;
9797

98+
/**
99+
* Icon to be used for the marker.
100+
* See: https://developers.google.com/maps/documentation/javascript/reference/marker#Icon
101+
*/
102+
@Input()
103+
set icon(icon: string | google.maps.Icon | google.maps.Symbol) {
104+
this._icon = icon;
105+
}
106+
private _icon: string | google.maps.Icon | google.maps.Symbol;
107+
98108
/**
99109
* See
100110
* developers.google.com/maps/documentation/javascript/reference/marker#Marker.animation_changed
@@ -276,7 +286,7 @@ export class MapMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint {
276286
}
277287

278288
ngOnChanges(changes: SimpleChanges) {
279-
const {marker, _title, _position, _label, _clickable} = this;
289+
const {marker, _title, _position, _label, _clickable, _icon} = this;
280290

281291
if (marker) {
282292
if (changes['options']) {
@@ -298,6 +308,10 @@ export class MapMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint {
298308
if (changes['clickable'] && _clickable !== undefined) {
299309
marker.setClickable(_clickable);
300310
}
311+
312+
if (changes['icon'] && _icon) {
313+
marker.setIcon(_icon);
314+
}
301315
}
302316
}
303317

@@ -430,8 +444,9 @@ export class MapMarker implements OnInit, OnChanges, OnDestroy, MapAnchorPoint {
430444
title: this._title || options.title,
431445
position: this._position || options.position,
432446
label: this._label || options.label,
433-
clickable: this._clickable !== undefined ? this._clickable : options.clickable,
447+
clickable: this._clickable ?? options.clickable,
434448
map: this._googleMap.googleMap,
449+
icon: this._icon || options.icon
435450
};
436451
}
437452

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ export declare class MapMarker implements OnInit, OnChanges, OnDestroy, MapAncho
234234
cursorChanged: Observable<void>;
235235
draggableChanged: Observable<void>;
236236
flatChanged: Observable<void>;
237+
set icon(icon: string | google.maps.Icon | google.maps.Symbol);
237238
iconChanged: Observable<void>;
238239
set label(label: string | google.maps.MarkerLabel);
239240
mapClick: Observable<google.maps.MapMouseEvent>;
@@ -272,7 +273,7 @@ export declare class MapMarker implements OnInit, OnChanges, OnDestroy, MapAncho
272273
ngOnChanges(changes: SimpleChanges): void;
273274
ngOnDestroy(): void;
274275
ngOnInit(): void;
275-
static ɵdir: i0.ɵɵDirectiveDefWithMeta<MapMarker, "map-marker", ["mapMarker"], { "title": "title"; "position": "position"; "label": "label"; "clickable": "clickable"; "options": "options"; }, { "animationChanged": "animationChanged"; "mapClick": "mapClick"; "clickableChanged": "clickableChanged"; "cursorChanged": "cursorChanged"; "mapDblclick": "mapDblclick"; "mapDrag": "mapDrag"; "mapDragend": "mapDragend"; "draggableChanged": "draggableChanged"; "mapDragstart": "mapDragstart"; "flatChanged": "flatChanged"; "iconChanged": "iconChanged"; "mapMousedown": "mapMousedown"; "mapMouseout": "mapMouseout"; "mapMouseover": "mapMouseover"; "mapMouseup": "mapMouseup"; "positionChanged": "positionChanged"; "mapRightclick": "mapRightclick"; "shapeChanged": "shapeChanged"; "titleChanged": "titleChanged"; "visibleChanged": "visibleChanged"; "zindexChanged": "zindexChanged"; }, never>;
276+
static ɵdir: i0.ɵɵDirectiveDefWithMeta<MapMarker, "map-marker", ["mapMarker"], { "title": "title"; "position": "position"; "label": "label"; "clickable": "clickable"; "options": "options"; "icon": "icon"; }, { "animationChanged": "animationChanged"; "mapClick": "mapClick"; "clickableChanged": "clickableChanged"; "cursorChanged": "cursorChanged"; "mapDblclick": "mapDblclick"; "mapDrag": "mapDrag"; "mapDragend": "mapDragend"; "draggableChanged": "draggableChanged"; "mapDragstart": "mapDragstart"; "flatChanged": "flatChanged"; "iconChanged": "iconChanged"; "mapMousedown": "mapMousedown"; "mapMouseout": "mapMouseout"; "mapMouseover": "mapMouseover"; "mapMouseup": "mapMouseup"; "positionChanged": "positionChanged"; "mapRightclick": "mapRightclick"; "shapeChanged": "shapeChanged"; "titleChanged": "titleChanged"; "visibleChanged": "visibleChanged"; "zindexChanged": "zindexChanged"; }, never>;
276277
static ɵfac: i0.ɵɵFactoryDef<MapMarker, never>;
277278
}
278279

0 commit comments

Comments
 (0)