【问题标题】:Enable drawing features by keeping pressing ctrl in Openlayers通过在 Openlayers 中按住 ctrl 来启用绘图功能
【发布时间】:2017-01-16 08:58:39
【问题描述】:

我正在尝试添加一个在按住“ctrl”时启用绘图功能的功能,例如一个圆圈。

var source = new ol.source.Vector({wrapX: false})
var draw = new ol.interaction.Draw({
    source: source,
    type: 'Circle'
})

document.addEventListener('keydown', function(e) {
    if (e.keyCode === 17) {
        draw.setActive(true);
        map.addInteraction(draw)
    }
})

document.addEventListener('keydown', function(e) {
    if (e.keyCode === 17) {
        draw.setActive(false);
        map.removeInteraction(draw)
    }
})

当我按下 ctrl 时这不起作用,但如果我修改代码以检测 shift 按键以启用绘图功能,则工作正常。

我想我一定错过了什么。你能告诉我为什么按 ctrl 不起作用但 shift 工作正常吗?谢谢。

【问题讨论】:

    标签: javascript openlayers openlayers-3


    【解决方案1】:

    我找到了解决方案。这不是您想要的,但它确实接近您的需要。

    我如何绕过你的问题:

    如果您想按住控制键进行绘图,它对我也不起作用。交互似乎有效,但实际上没有绘图发生。

    但是如果你使用 CTRL 键作为工具。我的意思是,如果你按一次开始绘图,绘制你的形状,然后再按一次 CTRL 键停止它,那么我可以为你做。

    您可以使用以下代码:

    这是地图代码:

    var map = new ol.Map({
                target: 'map',
                layers: [new ol.layer.Tile({source: new ol.source.OSM()})],
                view: new ol.View({
                    center: ol.proj.transform([0, 20], 'EPSG:4326', 'EPSG:3857'),
                    zoom: 3
                })
    });
    

    这是我们存储绘图特征的特征层/集合

    var drawingFeatures = new ol.Collection();
    // The layer of these drawing features
    var drawingLayer = new ol.layer.Vector({
         source: new ol.source.Vector({features: drawingFeatures}),
         style: new ol.style.Style({
             fill: new ol.style.Fill({
                 color: 'rgba(255, 255, 255, 0.2)'
             }),
             stroke: new ol.style.Stroke({
                  color: '#ffcc33',
                  width: 2
             }),
             image: new ol.style.Circle({
                  radius: 7,
                  fill: new ol.style.Fill({
                       color: '#ffcc33'
                  })
             })
         })
    });
    drawingLayer.setMap(map);               // put the layer in our map
    

    这是绘图交互:

    var draw = new ol.interaction.Draw({
        source: new ol.source.Vector({wrapX: false}),
        type: 'Circle',                     // type of draw
        features: drawingFeatures           // features where to store drawings
    });
    

    最后是启动/停止绘图交互的监听器:

    var drawingFlag = false;                // flag for start drawing
    document.addEventListener('keydown', function(e) {
        // the key code of the key we must hit to draw features
        // CTRL = 17
        // Note that SHIFT key is already used for zooming area by default
        var keyCode = 17; 
    
        if (e.keyCode === keyCode) {        // if its the good keycode
    
            if(drawingFlag === false) {
                //console.info("Start drawing");
                draw.setActive(true);       // activate draw
                map.addInteraction(draw);   // add interaction draw
                drawingFlag = true;         // start drawing flag
            }
            else {
                //console.info("Stop drawing");
                draw.setActive(false);      // deactivate draw
                map.removeInteraction(draw);// remove interaction draw
                drawingFlag = false;        // stop drawing flag
            }
        }
    });
    

    这对我很有用!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多