10

i'm using OpenLayers to display custom OSM maps on my website.

I've some points to respect: the map have to be fix (meaning that we can't drag it or zoom it).

I have a problem with the zoom, i can't manage to disable zoom with the mouse. Does anyone has a tip?

map = new OpenLayers.Map('map');
map.events.remove("move");
map.events.remove("movestart");
map.events.remove("moveend");
map.events.remove("zoomend");
map.events.remove("mouseover");
map.events.remove("mouseout");
map.events.remove("mousemove");
map.events.remove("zoomstart");
var nav = new OpenLayers.Control.Navigation({
  defaultDblClick: function(event) { return ; }
});
map[index].addControl(nav);

Also, if someone has a tip to remove all Navigation events easier than that, it would be greatly appreciated.

6 Answers 6

17

Disable the default controls on your map by passing an empty array:

var map = new OpenLayers.Map('map', { controls: [] });
0
11

For OpenLayers3 the interaction array also needs to be empty.

var map = new ol.Map({
  controls: [],
  interactions: []
});
7

Simplifying approach of Mahdi results in

var i, l, c = map.getControlsBy( "zoomWheelEnabled", true );
for ( i = 0, l = c.length; i < l; i++ ) {
    c[i].disableZoomWheel();
}

This way disabling zoom on mouse wheel doesn't require to customize options on constructing map e.g. by creating map without any control (though this was somewhat requested by Lght). In addition re-enabling zoom works equivalently.

In addition, by searching controls matching enabled property zoomWheelEnabled rather than class name it's supporting custom controls derived from OpenLayers.Control.Navigation.

3
  • I can't test this, but it seems to be another good way of doing that in my opinion. Even though i wanted to know how you guys were disabling the mousewheel zoom in OpenLayers, my goal was to remove everything (the zoom was the only thing left). Thanks for you answer, i hope it will help more people!
    – GeoffreyB
    Commented Jul 8, 2014 at 8:40
  • That worked perfectly! Much more straight forward from any other solution seen till now.
    – user1919
    Commented Mar 12, 2015 at 11:35
  • And to enable again: var i, l, c = map.getControlsBy("zoomWheelEnabled", false); for ( i = 0, l = c.length; i < l; i++ ) { c[i].enableZoomWheel(); }
    – igneosaur
    Commented Nov 24, 2015 at 11:06
3

You can do the following also:

map = new OpenLayers.Map({
    // options here ...
}

var Navigation = new OpenLayers.Control.Navigation({
    'zoomWheelEnabled': false,
    'defaultDblClick': function ( event ) { 
        return; 
     }
});

map.addControl(Navigation);

var NavigationControls = map.getControlsByClass('OpenLayers.Control.Navigation')
  , i;

for ( i = 0; i < NavigationControls.length; i++ ) {
    NavigationControls[i].disableZoomWheel();
}

Found here.

For other options like disable dragging, you can take a look at the documentation and customize the above code.

2

Here is another easy way to restrict the zoom event based on some logic. Because OpenLayers doesnt provide a 'beforezoom'

map.zoomToProxy = map.zoomTo;
map.zoomTo =  function (zoom,xy){
// if you want zoom to go through call
map.zoomToProxy(zoom,xy); 
//else do nothing and map wont zoom
};

How this works:

For any kind of zooming activity, OpenLayers API ultimately calls the function called zoomTo. So before overriding it, we copy that function to a new function called 'zoomToProxy'. The we override it and add our conditional zoom logic. If we want the zoom to happen we just call new proxy function :)

0
0

You could reset the controls array and then add the Zoom and TouchNavigation to it.

var map_controls = [];
map_controls.push( new OpenLayers.Control.Zoom() );
map_controls.push( new OpenLayers.Control.TouchNavigation() );
var map = new OpenLayers.Map({
  div: "map",
  controls: map_controls
});

Hope it helps ! :)

Not the answer you're looking for? Browse other questions tagged or ask your own question.