Skip to content

Commit c9700df

Browse files
dmitry-stepanenkowagnermaciel
authored andcommitted
fix(google-maps): rendering blank if custom options with no zoom are provided (#20882)
If an options object without a 'zoom' is passed to the Google Maps API, it'll render a grey rectangle which looks broken. These changes make sure that we always pass in a 'zoom' so map is rendered. (cherry picked from commit f0a4e59)
1 parent 6b13154 commit c9700df

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,30 @@ describe('GoogleMap', () => {
181181
expect(mapConstructorSpy.calls.mostRecent()?.args[1].center).toBeTruthy();
182182
});
183183

184+
it('should set a default zoom level if the custom options do not provide one', () => {
185+
const options = {};
186+
mapSpy = createMapSpy(options);
187+
mapConstructorSpy = createMapConstructorSpy(mapSpy).and.callThrough();
188+
189+
const fixture = TestBed.createComponent(TestApp);
190+
fixture.componentInstance.options = options;
191+
fixture.detectChanges();
192+
193+
expect(mapConstructorSpy.calls.mostRecent()?.args[1].zoom).toEqual(DEFAULT_OPTIONS.zoom);
194+
});
195+
196+
it('should not set a default zoom level if the custom options provide "zoom: 0"', () => {
197+
const options = {zoom: 0};
198+
mapSpy = createMapSpy(options);
199+
mapConstructorSpy = createMapConstructorSpy(mapSpy).and.callThrough();
200+
201+
const fixture = TestBed.createComponent(TestApp);
202+
fixture.componentInstance.options = options;
203+
fixture.detectChanges();
204+
205+
expect(mapConstructorSpy.calls.mostRecent()?.args[1].zoom).toEqual(0);
206+
});
207+
184208
it('gives precedence to center and zoom over options', () => {
185209
const inputOptions = {center: {lat: 3, lng: 5}, zoom: 7, heading: 170};
186210
const correctedOptions = {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,10 +455,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
455455
.pipe(map(([options, center, zoom]) => {
456456
const combinedOptions: google.maps.MapOptions = {
457457
...options,
458-
// It's important that we set **some** kind of `center`, otherwise
458+
// It's important that we set **some** kind of `center` and `zoom`, otherwise
459459
// Google Maps will render a blank rectangle which looks broken.
460460
center: center || options.center || DEFAULT_OPTIONS.center,
461-
zoom: zoom !== undefined ? zoom : options.zoom,
461+
zoom: zoom ?? options.zoom ?? DEFAULT_OPTIONS.zoom,
462462
mapTypeId: this.mapTypeId
463463
};
464464
return combinedOptions;

0 commit comments

Comments
 (0)