Javascript Openlayers-在地图初始化后启用交互
我使用的是openlayers 6.5 我是这样开始的:Javascript Openlayers-在地图初始化后启用交互,javascript,openlayers,Javascript,Openlayers,我使用的是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.fromLon
/*** 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.AltShiftDragRotate) {
interaction.setActive(true);
}
});
错误:instanceof'右侧不是对象
一些帮助?而不是使用
ol.interaction.defaults
和false
选项(选项名称应为camelCase
格式),您应该使用
var interactions = ol.interaction.defaults();
interactions.forEach((interaction) => {
interaction.setActive(false);
});
因为将选项设置为false会排除某些交互,而不是将其设置为禁用
altShiftDragRotate
是用于禁用(或可能排除)DragRotate交互的选项名称,因此所需的交互类是ol.interaction.DragRotate
interactions.forEach((interaction) => {
if (interaction instanceof ol.interaction.DragRotate) {
interaction.setActive(true);
}
});
默认设置中的交互与options@Mike是的,这是真的,但这似乎不是问题。将
AltShiftDragRotate
替换为AltShiftDragRotate
无效。我也犯了同样的错误。
interactions.forEach((interaction) => {
if (interaction instanceof ol.interaction.DragRotate) {
interaction.setActive(true);
}
});