Kokesh
Kokesh

Reputation: 3263

API V3 - set the zoom -1 from the zoom required to fit the markers

I would like my map to initially zoom out -1 from zoom determined by bounds. If I do the following, it just zooms in/out, but I want to set the zoom level "a little bigger".

map.fitBounds(bounds);

For the reasons quite well known I can't use map.setZoom afterwards.

Is there some simple way to do this?

Upvotes: 0

Views: 87

Answers (2)

Seain Malkin
Seain Malkin

Reputation: 2303

The best thing to do is to create an event listener for the map idle event. Once fitbounds has done its thing, the idle event will be triggered.

If you need to do it every time you fit the bounds then create a custom fitbounds method

var fitBounds = function(map, bounds) {
   google.maps.event.addListenerOnce(map, 'idle', function() {
       this.setZoom(this.getZoom() - 1);
   });
   map.fitBounds(bounds);
}

Notice we use the addListenerOnce method instead of the normal addListener.

The problem with this method though is you will still see the initial bounds set for a split second before the map zooms out.

Upvotes: 1

duncan
duncan

Reputation: 31912

You could use an event listener on zoom_changed:

google.maps.event.addListenerOnce(map, 'zoom_changed', function() {
    var zoomLevel = map.getZoom();

    map.setZoom(zoomLevel-1);
});

Upvotes: 0

Related Questions