google maps API 3 zooms out on fitBounds

前端 未结 7 1511
心在旅途
心在旅途 2021-01-11 14:48

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

7条回答
  •  花落未央
    2021-01-11 15:24

    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.

    Show Map 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.

提交回复
热议问题