fitBounds() shows whole earth (if map is first hidden and then shown)

前端 未结 5 1805
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-15 06:43

I have a bunch or markers, and I want to show only the area containing them. I found a long list of similar questions (see at the bottom of the post for some), but none of the s

5条回答
  •  温柔的废话
    2021-02-15 07:22

    To expand a bit on @JayThakkar 's answer, this worked for me

    google.maps.event.addListenerOnce(map, 'idle', function(){
        map.fitBounds(bounds);
    });
    

    The addListenerOnce function removes the need to call google.maps.event.removeListener(listener);. And calling map.fitBounds(bounds) inside the listener let us use the calculated bounds's zoom level.

提交回复
热议问题