I\'ve run into a problem where calling map.fitBounds seems to zoom out. I\'m trying to use the backbone.js router to save the map bounds in the url. I want to be able to boo
I noticed this behavior when using fitBounds()
on a hidden map view (using a front-end rendering framework, like Angular). Because this was a mobile view, I was showing a list of locations first, and was populating the markers on a hidden map as the list was loading (a concurrent load). So when the user wants to view the map, and they switch the segment view/tab/whatever, the map would show with the markers already loaded and in view. But this bugged out and zoomed to show the complete world in the map.
fitBounds()
needs the map to be loaded in the view because it uses the maps dimensions in order to resize the map. If the map is hidden, it zooms the map until the complete map is shown.
The solution was simple: when the view was switched, call fitBounds()
again.
public showMap() {
this.view = 'map';
setTimeout(() => {
if (typeof this.map !== 'undefined') this.map.fitBounds(this.bounds);
});
}
Note: I wrapped a setTimeout
around the fitBounds()
call so that Angular can finish the lifecycle and render the map before it is called.