Reputation: 7003
Is it possible to keep the same map center on the zoom in/out events? whether it is with the scroll wheel, double clicking or with the zoom controls it should behave the same.
I've noticed there is a bias towards the mouse pointer position on the map when zooming in/out with the Scroll Wheel, I haven't found any options on the Docs, but overriding that bias would be ideal.
here is a simple fiddle of how i am initializing my map.
var map = new google.maps.Map(document.getElementById("map_div"), {
center: new google.maps.LatLng(33.808678, -117.918921),
zoom: 15,
disableDefaultUI: true
});
Upvotes: 1
Views: 1367
Reputation: 7003
Following @Adam's suggestion on the comments, I disabled the scroll wheel zoom, plus the double click zoom when instantiating the map and added event listeners to handle those events with my own implementation.
Here is a working fiddle and the code explained:
/*
* declare map as a global variable
*/
var map;
/*
* use google maps api built-in mechanism to attach dom events
*/
google.maps.event.addDomListener(window, "load", function() {
var myMap = document.getElementById("map_div");
/*
* create map
*/
var map = new google.maps.Map(myMap, {
center: new google.maps.LatLng(33.808678, -117.918921),
zoom: 15,
disableDefaultUI: true,
scrollwheel: false,
disableDoubleClickZoom: true
});
//function that will handle the wheelEvent
function wheelEvent(event) {
var e = window.event || e; // old IE support
var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail))); //to know whether it was wheel up or down
map.setZoom(map.getZoom() + delta);
}
function zoomIn() {
map.setZoom(map.getZoom() + 1);
}
//add a normal event listener on the map container
myMap.addEventListener('mousewheel', wheelEvent, true);
myMap.addEventListener('DOMMouseScroll', wheelEvent, true);
//same with the double click
myMap.addEventListener('dblclick', zoomIn, true);
//add center changed listener for testing
google.maps.event.addListener(map, 'center_changed', function() {
console.log("this is what i'm trying to avoid");
});
});
Upvotes: 5