Reputation: 5989
Can I limit the zoom Google map api to individual zoom ? I mean can I have zoom 2 and 6 only and not 2-3-4-5-6 I saw that I have min zoom and max zoom but I don't want that the zoom will be in range only in single .
Regards, Yossi
Upvotes: 2
Views: 4010
Reputation: 21630
You can add a listener for the zoom_changed event, check the zoom, and set the zoom depending on whether it's closer to 2 or closer to 6.
(assuming a 'map' variable holds an instance of the map)
google.maps.event.addListener(map, 'zoom_changed', function() {
var currentZoom = map.getZoom();
if (currentZoom !== 2 && currentZoom !==6) {
if (currentZoom <=4) {
map.setZoom(2);
} else {
map.setZoom(6);
}
}
});
Upvotes: 1
Reputation: 12973
You can only mimic this. The API doesn't allow you to miss out zooms.
The zoom_changed
event no longer tells you anything about the change. In Version 2, you could know the old zoom-level and the new level.
Accordingly, you have to guess in Version 3.
If you are at zoom 2 and the user zooms in, the API will go to zoom 3. So you can intercept the zoom_changed
event, test whether the map is now at level 3 and put it to level 6. You can do the same in the other direction: if the map should be at level 5 (from 6), make it 2.
// Untested code
google.maps.event.addListener(map,'zoom_changed',function () {
if (map.getZoom() == 3) map.setZoom(6);
if (map.getZoom() == 5) map.setZoom(2);
});
Note that this will not help if the user uses the zoom slider and moves directly from 2 to 5: the event handler will see the current zoom as 5, assume it's moved only one step (from 6) and set it back to 2. It also won't help if the slider is used to get to zoom 4. You need to decide what should happen in these circumstances. You may want to use a limited set of map controls which doesn't have the slider.
Upvotes: 3
Reputation: 6839
well you can use the function getZoom() on click on the map. check if the zoom level is changed. then use setZoom for setting the appropriate zoom level click here for help
Upvotes: 1