1

There is a problem to set interaction for the map object in OpenLayers. I've used this code but it's not working:

var osmLayer = new ol.layer.Tile({
    source: new ol.source.OSM()
});
var interaction = new ol.interaction.DragRotateAndZoom();
var center = ol.proj.transform([51.34, 35.65], 'EPSG:4326', 'EPSG:3857');

var view = new ol.View({
    center: center,
    zoom: 10
});

var map = new ol.Map({
    target: 'map',
    layers: [osmLayer],
    interactions: [interaction],     
    view: view
});

But when I use this template the interaction is just fine. what's wrong with first approach?

var map = new ol.Map({
    target: 'map',
    layers: [osmLayer],
    view: view
});
map.addInteraction(interaction);

1 Answer 1

2

You cannot simply pass an array with interactions, but you have to use ol.interaction.defaults().extend, like this:

var interaction = new ol.interaction.DragRotateAndZoom();

var map = new ol.Map({
    target: 'map',
    layers: [osmLayer],
    interactions: ol.interaction.defaults().extend([
        interaction
    ]),
    view: view
});

Then it will work without using map.addInteraction(interaction);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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