Openlayers - 在地图初始化后启用交互

Openlayers - Enable interaction after map initialization

我正在使用 openlayers 6.5。

我是这样开始的:

/*** Set the map ***/
map = new ol.Map({
    target: map_element,
    layers: [
        new ol.layer.Tile({
            source: new ol.source.OSM()
        })
    ],
    view: new ol.View({
        center: ol.proj.fromLonLat([
            '24.6859225',
            '45.9852129',
        ]),
        zoom: 6,
    }),
    interactions: ol.interaction.defaults({
        'DragPan': false,
        'Keyboard': false,
        'PinchZoom': false,
        'ZoomDelta': false,
        'PinchRotate': false,
        'OnFocusOnly': false,
        'ZoomDuration': false,
        'ShiftDragZoom': false,
        'MouseWheelZoom': false,
        'DoubleClickZoom': false,
        'AltShiftDragRotate': false
    }),
    controls: [],
});

我希望能够在地图初始化后启用和禁用每个交互。

我在堆栈上找到了下一个代码(启用 DragPan):

map.getInteractions().forEach((interaction) => {
    if (interaction instanceof ol.interaction.DragPan) {
        interaction.setActive(true);
    }
});

但是下一个不行:

map.getInteractions().forEach((interaction) => {
    if (interaction instanceof ol.interaction.AltShiftDragRotate) {
        interaction.setActive(true);
    }
});

错误:Right-hand side of 'instanceof' is not an object

有帮助吗?

而不是使用 ol.interaction.defaultsfalse 选项(选项名称应该是 camelCase 格式)你应该使用

var interactions = ol.interaction.defaults();
interactions.forEach((interaction) => {
    interaction.setActive(false);
});

因为将选项设置为 false 会排除一些交互而不是将它们设置为禁用

altShiftDragRotate 是用于禁用(或可能排除)DragRotate 交互的选项名称,因此需要的交互 class 是 ol.interaction.DragRotate

interactions.forEach((interaction) => {
    if (interaction instanceof ol.interaction.DragRotate) {
        interaction.setActive(true);
    }
});