Skip to content

Commit

Permalink
fix(google-maps): ensure that a mapTypeId is always passed in (#22098)
Browse files Browse the repository at this point in the history
We pass in `mapTypeId` as undefined if the provided options don't have it. This seems to
trigger a bug in Google Maps where it stops loading map tiles.

These changes fix the issue by always providing a `mapTypeId`.

Fixes #22082.

(cherry picked from commit 7c9b83a)
  • Loading branch information
crisbeto authored and andrewseguin committed Mar 23, 2021
1 parent 3ead2cd commit b79a2d5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
21 changes: 10 additions & 11 deletions src/google-maps/google-map/google-map.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ describe('GoogleMap', () => {
const container = fixture.debugElement.query(By.css('div'))!;
expect(container.nativeElement.style.height).toBe(DEFAULT_HEIGHT);
expect(container.nativeElement.style.width).toBe(DEFAULT_WIDTH);
expect(mapConstructorSpy).toHaveBeenCalledWith(container.nativeElement, {
...DEFAULT_OPTIONS,
mapTypeId: undefined
});
expect(mapConstructorSpy).toHaveBeenCalledWith(container.nativeElement, DEFAULT_OPTIONS);
});

it('sets height and width of the map', () => {
Expand All @@ -81,10 +78,7 @@ describe('GoogleMap', () => {
const container = fixture.debugElement.query(By.css('div'))!;
expect(container.nativeElement.style.height).toBe('750px');
expect(container.nativeElement.style.width).toBe('400px');
expect(mapConstructorSpy).toHaveBeenCalledWith(container.nativeElement, {
...DEFAULT_OPTIONS,
mapTypeId: undefined
});
expect(mapConstructorSpy).toHaveBeenCalledWith(container.nativeElement, DEFAULT_OPTIONS);

fixture.componentInstance.height = '650px';
fixture.componentInstance.width = '350px';
Expand Down Expand Up @@ -131,7 +125,7 @@ describe('GoogleMap', () => {
});

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

Expand All @@ -152,7 +146,12 @@ describe('GoogleMap', () => {
});

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

Expand Down Expand Up @@ -211,7 +210,7 @@ describe('GoogleMap', () => {
center: {lat: 12, lng: 15},
zoom: 5,
heading: 170,
mapTypeId: undefined
mapTypeId: DEFAULT_OPTIONS.mapTypeId
};
mapSpy = createMapSpy(correctedOptions);
mapConstructorSpy = createMapConstructorSpy(mapSpy);
Expand Down
8 changes: 6 additions & 2 deletions src/google-maps/google-map/google-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ interface GoogleMapsWindow extends Window {
/** default options set to the Googleplex */
export const DEFAULT_OPTIONS: google.maps.MapOptions = {
center: {lat: 37.421995, lng: -122.084092},
zoom: 17
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 */
Expand Down Expand Up @@ -466,7 +468,9 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
// Google Maps will render a blank rectangle which looks broken.
center: this._center || options.center || DEFAULT_OPTIONS.center,
zoom: this._zoom ?? options.zoom ?? DEFAULT_OPTIONS.zoom,
mapTypeId: this.mapTypeId || options.mapTypeId
// Passing in an undefined `mapTypeId` seems to break tile loading
// so make sure that we have some kind of default (see #22082).
mapTypeId: this.mapTypeId || options.mapTypeId || DEFAULT_OPTIONS.mapTypeId
};
}

Expand Down

0 comments on commit b79a2d5

Please sign in to comment.