Single Entity Single Entity - 7 months ago 13
Javascript Question

Open Layers 3 disable pinch rotate after map is loaded

I would like an option in my application which allows pinch rotate to be disabled when the user desires it.

I have a map:

map_object = new ol.Map({
target: 'map',
controls: controls_list,
interactions: interactions_list,
overlays: [overlay],
layers: vector_layers,
view: view
});


You'll notice that I have defined interactions in the usual way in the map definition. My interactions_list is as follows:

var interactions_list = ol.interaction.defaults({altShiftDragRotate:false, pinchRotate:true, dragPan:true});


How do I disable pinch rotate after the map object has been created so that map rotation is disabled after the map is loaded and displayed.

Answer

If you use OpenLayers v3.1.1 you can enable/disable an interaction by calling setActive(true)/setActive(false) on the interaction.

First you need to find the PinchRotate interaction in the interactions collection:

var interactions = map.getInteractions().getArray();
var pinchRotateInteraction = interactions.filter(function(interaction) {
  return interaction instanceof ol.interaction.PinchRotate;
})[0];

You can then enable and disable the interaction as needed:

pinchRotateInteraction.setActive(false);
pinchRotateInteraction.setActive(true);
Comments