Is there a way to disable rotation in OpenLayers 3?
Asked Answered
N

2

12

I am currently upgrading my OpenLayers 2 Mapview to OpenLayers 3. I really like the new OpenLayers client, but i wanted to deactivate the ability to rotate the map on mobile devices (rotating with 2 fingers).

But I can't find any settings for this. Is this not possible or am I only to dumb to find the setting?

I am using the current release version (3.0.0) of the openlayers javascript client. (https://github.com/openlayers/ol3/releases/tag/v3.0.0)

Neomineomycin answered 4/9, 2014 at 15:18 Comment(0)
F
35

Yes there is a way to deactivate the ability to rotate the map.

You need to customize the interactions of the ol.Map object. Either you use the ol.interaction.defaults function to create an ol.Collection with interactions or you create an array with only the interactions you want. Then you can pass it to the constructor of ol.Map.

Using the ol.interaction.defaults function (http://openlayers.org/en/master/apidoc/ol.interaction.html#defaults):

var interactions = ol.interaction.defaults({altShiftDragRotate:false, pinchRotate:false}); 
var map = new ol.Map {
    interactions: interactions
};

The first line creates all default interactions but the ability to rotate via keyboard+mouse and using fingers on a mobile device.

You maybe want to remove the ol.control.Rotate then, too. (This is the needle in the upper right which is used to reset the rotation and only appears if the map is rotated). Works the same way.

Creating controls without compass via ol.control.defaults (http://openlayers.org/en/master/apidoc/ol.control.html#defaults)

var controls = ol.control.defaults({rotate: false});

'Full' code:

var controls = ol.control.defaults({rotate: false}); 
var interactions = ol.interaction.defaults({altShiftDragRotate:false, pinchRotate:false});

var map = new ol.Map {
    controls: controls,
    interactions: interactions
};
Favoritism answered 5/9, 2014 at 9:2 Comment(2)
Very helpful and complete explanationUpshot
can u please say what exactly openlayer3 does with google map please helpTelegony
E
12

In the current version of OpenLayers 3 you can simply disable the enableRotation flag of the view object:

view: new ol.View({
    ...
    enableRotation: false
})
Elocution answered 29/1, 2017 at 17:36 Comment(1)
Yes, but it do not remove ol.control.Rotate.Fullblown

© 2022 - 2024 — McMap. All rights reserved.