Skip to content

Commit 0b9160b

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 9f73933 commit 0b9160b

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
@@ -448,10 +448,10 @@ export class GoogleMap implements OnChanges, OnInit, OnDestroy {
448448
.pipe(map(([options, center, zoom]) => {
449449
const combinedOptions: google.maps.MapOptions = {
450450
...options,
451-
// It's important that we set **some** kind of `center`, otherwise
451+
// It's important that we set **some** kind of `center` and `zoom`, otherwise
452452
// Google Maps will render a blank rectangle which looks broken.
453453
center: center || options.center || DEFAULT_OPTIONS.center,
454-
zoom: zoom !== undefined ? zoom : options.zoom,
454+
zoom: zoom ?? options.zoom ?? DEFAULT_OPTIONS.zoom,
455455
mapTypeId: this.mapTypeId
456456
};
457457
return combinedOptions;

0 commit comments

Comments
 (0)