Ryan
Ryan

Reputation: 6866

Why isn't Google Maps changing zoom when asked to do so?

I've got this bit of code here:

function ShowSelection(addr){
    var latLngBounds;

    geocoder.geocode({ "address": addr }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {

            if (selectionArea === undefined || selectionArea === null) {
                selectionArea = new google.maps.Rectangle({
                    map: map
                });
            }

            latLngBounds = results[0].geometry.bounds;
            selectionArea.setBounds(latLngBounds);

            map.panTo(latLngBounds.getCenter());
            map.setZoom(12);
        }
        else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}

My problem is that when I call map.setZoom(12) the map just stays at the same zoom level it was already at.

I guess a better solution would be to set it to a zoom level that shows the full selectionArea in the map window.

So I guess this is sort of a double question:

  1. How do I get the zoom level to change when calling setZoom()?
  2. How would I set the zoom level so that a Rectangle drawn on the map would completely fit in the bounds of the map div? (The desired effect I'm looking for is to have the map pan to the center point of the selectionArea and have the bounds of the map be larger than the bounds of the selection area i.e. the whole selectionArea is visible on the map w/o scrolling.

Upvotes: 1

Views: 654

Answers (1)

Francesco
Francesco

Reputation: 443

I'm not sure I understood your problem. The code you posted geocodes an address and draws a rectangle with the same size of the map bounds. So...

How do I get the zoom level to change when calling setZoom()?

well... it seems ok. Check if your map zoom isn't already set at 12

How would I set the zoom level so that a Rectangle drawn on the map would completely fit in the bounds of the map div?

You can make map fits the bounds with

map.fitBounds(latLngBounds);

Upvotes: 2

Related Questions