cpcdev
cpcdev

Reputation: 1222

Google Maps JavaScript API change zoom level after search

I would like to change the zoom level and min/max zoom levels AFTER a search is performed:

function initMap() {

    var myLatLng = {
        lat: 38.5803844, 
        lng: -121.50024189999999
    };

    map = new google.maps.Map(document.getElementById('map'), {
      zoom: 17,
      minZoom: 16,
      maxZoom: 17,
      center: myLatLng,
      mapTypeControl: false
    });

}

function getDirectionsHere(lat,lng) {

    map.setMinZoom(10);
    map.setZoom(10);

}

The code above changes the zoom level when a user performs a search, however it only zooms out as far as what was initially set in the initMap function (16).

map.setMinZoom is not a function it seems.

How can I accomplish this?

Upvotes: 4

Views: 2221

Answers (2)

tatsuya kanemoto
tatsuya kanemoto

Reputation: 2028

Maximum zoom level is able to be retrieved using MaxZoomService.

So this should work.

function initMap() {

    var myLatLng = {
        lat: 38.5803844, 
        lng: -121.50024189999999
    };

    map = new google.maps.Map(document.getElementById('map'), {
      zoom: 17,
      minZoom: 16,
      maxZoom: 17,
      center: myLatLng,
      mapTypeControl: false
    });

    maxZoomService = new google.maps.MaxZoomService();
}

function getDirectionsHere(lat,lng) {

    maxZoomService.getMaxZoomAtLatLng({lat, lng}, function(response) {
        map.setOptions({ minZoom: 5, maxZoom: response.zoom });
        map.setZoom(10);
    });
}

Upvotes: 1

cpcdev
cpcdev

Reputation: 1222

maps.SetOptions did the trick:

map.setOptions({ minZoom: 5, maxZoom: 15 });

Upvotes: 2

Related Questions