Skip to content

Commit 2a6aae1

Browse files
authored
feat(google-maps): support setting the map type (#18578)
Adds support for setting the `mapTypeId` which determines the kind of map that will be shown (e.g. terrain map, roadmap etc). Fixes #18577.
1 parent 6275339 commit 2a6aae1

File tree

6 files changed

+67
-6
lines changed

6 files changed

+67
-6
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
[zoom]="zoom"
66
(mapClick)="handleClick($event)"
77
(mapMousemove)="handleMove($event)"
8-
(mapRightclick)="handleRightclick()">
8+
(mapRightclick)="handleRightclick()"
9+
[mapTypeId]="mapTypeId">
910
<map-marker #firstMarker [position]="center" (mapClick)="clickMarker(firstMarker)"></map-marker>
1011
<map-marker #marker
1112
*ngFor="let markerPosition of markerPositions"
@@ -22,6 +23,16 @@
2223
<p><label>Latitude:</label> {{display?.lat}}</p>
2324
<p><label>Longitude:</label> {{display?.lng}}</p>
2425

26+
<div>
27+
<label for="map-type">
28+
Select map type
29+
30+
<select id="map-type" (change)="mapTypeChanged($event)" #mapType>
31+
<option *ngFor="let type of mapTypeIds" [value]="type">{{type}}</option>
32+
</select>
33+
</label>
34+
</div>
35+
2536
<div>
2637
<label for="polyline-checkbox">
2738
Toggle Polyline

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ export class GoogleMapDemo {
6666
circleOptions: google.maps.CircleOptions =
6767
{center: CIRCLE_CENTER, radius: CIRCLE_RADIUS, strokeColor: 'grey', strokeOpacity: 0.8};
6868

69+
mapTypeId: google.maps.MapTypeId;
70+
mapTypeIds = [
71+
google.maps.MapTypeId.HYBRID,
72+
google.maps.MapTypeId.ROADMAP,
73+
google.maps.MapTypeId.SATELLITE,
74+
google.maps.MapTypeId.TERRAIN
75+
];
76+
6977
handleClick(event: google.maps.MouseEvent) {
7078
this.markerPositions.push(event.latLng.toJSON());
7179
}
@@ -130,4 +138,8 @@ export class GoogleMapDemo {
130138
radius: this.circle.getRadius(),
131139
};
132140
}
141+
142+
mapTypeChanged(event: Event) {
143+
this.mapTypeId = (event.target as HTMLSelectElement).value as unknown as google.maps.MapTypeId;
144+
}
133145
}

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ describe('GoogleMap', () => {
122122
});
123123

124124
it('sets center and zoom of the map', () => {
125-
const options = {center: {lat: 3, lng: 5}, zoom: 7};
125+
const options = {center: {lat: 3, lng: 5}, zoom: 7, mapTypeId: undefined};
126126
mapSpy = createMapSpy(options);
127127
mapConstructorSpy = createMapConstructorSpy(mapSpy).and.callThrough();
128128

@@ -143,7 +143,7 @@ describe('GoogleMap', () => {
143143
});
144144

145145
it('sets map options', () => {
146-
const options = {center: {lat: 3, lng: 5}, zoom: 7, draggable: false};
146+
const options = {center: {lat: 3, lng: 5}, zoom: 7, draggable: false, mapTypeId: undefined};
147147
mapSpy = createMapSpy(options);
148148
mapConstructorSpy = createMapConstructorSpy(mapSpy).and.callThrough();
149149

@@ -162,7 +162,12 @@ describe('GoogleMap', () => {
162162

163163
it('gives precedence to center and zoom over options', () => {
164164
const inputOptions = {center: {lat: 3, lng: 5}, zoom: 7, heading: 170};
165-
const correctedOptions = {center: {lat: 12, lng: 15}, zoom: 5, heading: 170};
165+
const correctedOptions = {
166+
center: {lat: 12, lng: 15},
167+
zoom: 5,
168+
heading: 170,
169+
mapTypeId: undefined
170+
};
166171
mapSpy = createMapSpy(correctedOptions);
167172
mapConstructorSpy = createMapConstructorSpy(mapSpy);
168173

@@ -280,6 +285,23 @@ describe('GoogleMap', () => {
280285
expect(addSpy).toHaveBeenCalledWith('projection_changed', jasmine.any(Function));
281286
subscription.unsubscribe();
282287
});
288+
289+
it('should set the map type', () => {
290+
mapSpy = createMapSpy(DEFAULT_OPTIONS);
291+
mapConstructorSpy = createMapConstructorSpy(mapSpy).and.callThrough();
292+
293+
const fixture = TestBed.createComponent(TestApp);
294+
fixture.componentInstance.mapTypeId = 'terrain' as unknown as google.maps.MapTypeId;
295+
fixture.detectChanges();
296+
297+
expect(mapConstructorSpy).toHaveBeenCalledWith(jasmine.any(HTMLElement),
298+
jasmine.objectContaining({mapTypeId: 'terrain'}));
299+
300+
fixture.componentInstance.mapTypeId = 'roadmap' as unknown as google.maps.MapTypeId;
301+
fixture.detectChanges();
302+
303+
expect(mapSpy.setMapTypeId).toHaveBeenCalledWith('roadmap');
304+
});
283305
});
284306

285307
@Component({
@@ -289,6 +311,7 @@ describe('GoogleMap', () => {
289311
[center]="center"
290312
[zoom]="zoom"
291313
[options]="options"
314+
[mapTypeId]="mapTypeId"
292315
(mapClick)="handleClick($event)"
293316
(centerChanged)="handleCenterChanged()"
294317
(mapRightclick)="handleRightclick($event)">
@@ -301,6 +324,7 @@ class TestApp {
301324
center?: google.maps.LatLngLiteral;
302325
zoom?: number;
303326
options?: google.maps.MapOptions;
327+
mapTypeId?: google.maps.MapTypeId;
304328

305329
handleClick(event: google.maps.MouseEvent) {}
306330
handleCenterChanged() {}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export interface UpdatedGoogleMap extends google.maps.Map {
4646
export const DEFAULT_OPTIONS: google.maps.MapOptions = {
4747
center: {lat: 37.421995, lng: -122.084092},
4848
zoom: 17,
49+
mapTypeId: undefined
4950
};
5051

5152
/** Arbitrary default height for the map element */
@@ -79,10 +80,18 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
7980
/** Whether we're currently rendering inside a browser. */
8081
_isBrowser: boolean;
8182

83+
/** Height of the map. */
8284
@Input() height: string | number = DEFAULT_HEIGHT;
8385

86+
/** Width of the map. */
8487
@Input() width: string | number = DEFAULT_WIDTH;
8588

89+
/**
90+
* Type of map that should be rendered. E.g. hybrid map, terrain map etc.
91+
* See: https://developers.google.com/maps/documentation/javascript/reference/map#MapTypeId
92+
*/
93+
@Input() mapTypeId: google.maps.MapTypeId | undefined;
94+
8695
@Input()
8796
set center(center: google.maps.LatLngLiteral|google.maps.LatLng) {
8897
this._center.next(center);
@@ -249,6 +258,9 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
249258

250259
ngOnChanges() {
251260
this._setSize();
261+
if (this._googleMap && this.mapTypeId) {
262+
this._googleMap.setMapTypeId(this.mapTypeId);
263+
}
252264
}
253265

254266
ngOnInit() {
@@ -447,6 +459,7 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
447459
...options,
448460
center: center || options.center,
449461
zoom: zoom !== undefined ? zoom : options.zoom,
462+
mapTypeId: this.mapTypeId
450463
};
451464
return combinedOptions;
452465
}));

src/google-maps/testing/fake-google-map-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function createMapSpy(options: google.maps.MapOptions): jasmine.SpyObj<Up
2828
const mapSpy = jasmine.createSpyObj('google.maps.Map', [
2929
'setOptions', 'setCenter', 'setZoom', 'setMap', 'addListener', 'fitBounds', 'panBy', 'panTo',
3030
'panToBounds', 'getBounds', 'getCenter', 'getClickableIcons', 'getHeading', 'getMapTypeId',
31-
'getProjection', 'getStreetView', 'getTilt', 'getZoom'
31+
'getProjection', 'getStreetView', 'getTilt', 'getZoom', 'setMapTypeId'
3232
]);
3333
mapSpy.addListener.and.returnValue({remove: () => {}});
3434
return mapSpy;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export declare class GoogleMap implements OnChanges, OnInit, OnDestroy {
1818
mapMouseout: Observable<google.maps.MouseEvent>;
1919
mapMouseover: Observable<google.maps.MouseEvent>;
2020
mapRightclick: Observable<google.maps.MouseEvent>;
21+
mapTypeId: google.maps.MapTypeId | undefined;
2122
get mapTypes(): google.maps.MapTypeRegistry;
2223
maptypeidChanged: Observable<void>;
2324
set options(options: google.maps.MapOptions);
@@ -46,7 +47,7 @@ export declare class GoogleMap implements OnChanges, OnInit, OnDestroy {
4647
panBy(x: number, y: number): void;
4748
panTo(latLng: google.maps.LatLng | google.maps.LatLngLiteral): void;
4849
panToBounds(latLngBounds: google.maps.LatLngBounds | google.maps.LatLngBoundsLiteral, padding?: number | google.maps.Padding): void;
49-
static ɵcmp: i0.ɵɵComponentDefWithMeta<GoogleMap, "google-map", never, { "height": "height"; "width": "width"; "center": "center"; "zoom": "zoom"; "options": "options"; }, { "boundsChanged": "boundsChanged"; "centerChanged": "centerChanged"; "mapClick": "mapClick"; "mapDblclick": "mapDblclick"; "mapDrag": "mapDrag"; "mapDragend": "mapDragend"; "mapDragstart": "mapDragstart"; "headingChanged": "headingChanged"; "idle": "idle"; "maptypeidChanged": "maptypeidChanged"; "mapMousemove": "mapMousemove"; "mapMouseout": "mapMouseout"; "mapMouseover": "mapMouseover"; "projectionChanged": "projectionChanged"; "mapRightclick": "mapRightclick"; "tilesloaded": "tilesloaded"; "tiltChanged": "tiltChanged"; "zoomChanged": "zoomChanged"; }, never>;
50+
static ɵcmp: i0.ɵɵComponentDefWithMeta<GoogleMap, "google-map", never, { "height": "height"; "width": "width"; "mapTypeId": "mapTypeId"; "center": "center"; "zoom": "zoom"; "options": "options"; }, { "boundsChanged": "boundsChanged"; "centerChanged": "centerChanged"; "mapClick": "mapClick"; "mapDblclick": "mapDblclick"; "mapDrag": "mapDrag"; "mapDragend": "mapDragend"; "mapDragstart": "mapDragstart"; "headingChanged": "headingChanged"; "idle": "idle"; "maptypeidChanged": "maptypeidChanged"; "mapMousemove": "mapMousemove"; "mapMouseout": "mapMouseout"; "mapMouseover": "mapMouseover"; "projectionChanged": "projectionChanged"; "mapRightclick": "mapRightclick"; "tilesloaded": "tilesloaded"; "tiltChanged": "tiltChanged"; "zoomChanged": "zoomChanged"; }, never>;
5051
static ɵfac: i0.ɵɵFactoryDef<GoogleMap>;
5152
}
5253

0 commit comments

Comments
 (0)