tags:

views:

1206

answers:

5

Hello,

I am working on a googlemap, which all works fine, apart from the fact I can't seem to set a max and min zoom (I would like to limit the levels to a couple of levels either way of a default zoom view)

I have tried using the map.getMimimumResolution, but this doesn't seem to work - any ideas ?

function initialize() {   
   var latlng = new google.maps.LatLng("<%=Html.Encode(Model.Field01.Field01) %>", "<%=Html.Encode(Model.Field01.Field03) %>");
    var myOptions = {
    zoom: 13,
        center: latlng,
        disableDefaultUI: false,
        navigationControl: true,
        navigationControlOptions: {style: google.maps.NavigationControlStyle.ZOOM_PAN},
        navigationControl: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP

    };
    var map = new google.maps.Map(document.getElementById("mapcontainer"), myOptions);

    map.getMinimumResolution = function() { return 6 }; 

    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        title: "<%=Model.AccomName %>"
      });
}

Any thoughts appreciated, Thanks

+1  A: 

getMinimumResolution is a GMapType Method, not a GMap2 Method. so instead of this:

map.getMinimumResolution = function() { return 6 };

You could do this:

var mt = map.getMapTypes();

for (var i=0; i<mt.length; i++) {
  mt[i].getMinimumResolution = function() {return 6;}
}
Mike Williams
thanks for the response, i have tried this out, it does not seem to recognise the getMapTypes as a function though - I have looked this up and like you have laid out, it should exist - can you think of why my map would not recognise getMapTypes ? I have dropped the code you supplied in after the declaration of the map object
ivor
Are you using Maps v3 (Labs)?It's not clear from your code snippets. I'd assumed Maps v2 because it has a maptype.getMinimumResolution Method, but if you're not getting map.getMapType then your problem could be that you're using API v3 as suggested in one of the other Answers.Here's the code working in one of my v2 tutorial maps:http://econym.org.uk/gmap/example_range.htm
Mike Williams
Thanks - the map was set up with v3 - I will look into getting these working now.
ivor
A: 

It looks like you're using the Maps API Version 3, which doesn't have a way to set the min and max resolutions. Not yet at least.

You could always listen for zoom changes and reset the zoom if it's not where you want it to be - but that would be pretty ugly. :)

Chris B
+1  A: 

As mentioned above, the only way I have so far found in V3 to do this is with an event listener. Here is the code (for maximum zoom level 12 as an example):

google.maps.event.addListener(map, "zoom_changed", function() {
    if (map.getZoom() > 12) map.setZoom(12);
});

Yes, ugly and dirty and wrong, I know.

FuzzyDunlop
A: 

Try this. Works fine for me

// force normal maps type
map.setMapType(G_NORMAL_MAP);

// define minimum and maximum zoom levels
G_NORMAL_MAP.getMinimumResolution = function() { return 0; }
G_NORMAL_MAP.getMaximumResolution = function() { return 19; }
baneizalfe
+2  A: 

Code below works smoothly for me. No flickering, glitches, etc. Maps API v3.

var inProcess = false;

function onZoomChanged() {
    if (inProcess) return;

    if (gmap.getZoom() > 16) {
        inProcess = true;
        gmap.setZoom(16);
        inProcess = false;
        return
    }
    else if (gmap.getZoom() < 10) {
        inProcess = true;
        gmap.setZoom(10);
        inProcess = false;
        return;
    }
}

google.maps.event.addListener(gmap, 'zoom_changed', onZoomChanged);