I noticed this behavior when using fitBounds() in a hidden map view (using fitBounds() a rendering interface such as Angular). Since this was a mobile view, I first showed a list of locations and filled in markers on a hidden map while the list was loading (simultaneous loading). Therefore, when the user wants to view the map and switch the segment view / tab / whatever, the map will be displayed with already loaded and visible markers. But it distorted and scaled up to show the whole world on a map.
<div [hidden]="view !== 'map'> <google-map></google-map> </div>
fitBounds() needs to load the map in the view, since it uses map sizes to resize the map. If the map is hidden, it zooms in on the map until a full map is displayed.
The solution was simple: when switching the view, call fitBounds() again.
<button (click)="showMap()">Show Map</button> public showMap() { this.view = 'map'; setTimeout(() => { if (typeof this.map !== 'undefined') this.map.fitBounds(this.bounds); }); }
Note. I wrapped setTimeout around fitBounds() so that Angular can complete the life cycle and display the map before it is called.
Kevin chung
source share