【问题标题】:OpenLayers 5 draw interactions - "n is not a constructor" at Draw.js:338OpenLayers 5 绘制交互 - Draw.js:338 中的“n 不是构造函数”
【发布时间】:2019-10-03 16:57:22
【问题描述】:

我正在尝试允许用户绘制和修改功能,遵循以下示例:https://openlayers.org/en/v5.3.0/examples/draw-and-modify-features.html - 但出现两个错误:

Uncaught TypeError: n is not a constructor
    at e.r [as geometryFunction_] (Draw.js:338)
    at e.startDrawing_ (Draw.js:700)
    at e.handleUpEvent (Draw.js:570)
    at e.handleEvent (Pointer.js:132)
    at e.handleEvent (Draw.js:524)
    at e.handleMapBrowserEvent (PluggableMap.js:933)
    at e (events.js:41)
    at e.dispatchEvent (Target.js:101)
    at e.handlePointerUp_ (MapBrowserEventHandler.js:166)
    at e (events.js:41)

Uncaught TypeError: Cannot read property 'getGeometry' of null
    at e.modifyDrawing_ (Draw.js:717)
    at e.handlePointerMove_ (Draw.js:618)
    at e.handleEvent (Draw.js:518)
    at e.handleMapBrowserEvent (PluggableMap.js:933)
    at e (events.js:41)
    at e.dispatchEvent (Target.js:101)
    at e.relayEvent_ (MapBrowserEventHandler.js:279)
    at e (events.js:41)
    at e.dispatchEvent (Target.js:101)
    at e.fireNativeEvent (PointerEventHandler.js:397)

当我单击开始绘制特征时,控制台中会出现第一个错误。然后蓝点停留在同一个地方,当我移动鼠标时,第二个错误反复触发。在第一次点击之后,点击地图不会做任何事情。

我在 Draw.js 中看不到任何明显的东西,据我所知,我的代码是完全一样的,除了一些小的修改以适应我的应用程序,就像我上面提到的例子一样。

我使用的是 OpenLayers 5.3.0,我通过 rawgit 加载:

<link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css">
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>

我的代码的其他相关部分如下 - 改编自上面提到的示例:

var drawSource = new ol.source.Vector();
var drawVector = new ol.layer.Vector({
  source: drawSource,
  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'
      })
    })
  })
});
map.addLayer(drawVector);

var modify = new ol.interaction.Modify({source: drawSource});

map.addInteraction(modify);

var draw, snap; // global so we can remove them later

function removeInteractions() {
  map.removeInteraction(draw);
  map.removeInteraction(snap);
}

function addInteractions() {
  draw = new ol.interaction.Draw({
    source: drawSource,
    type: ol.geom.Polygon
  });
  map.addInteraction(draw);
  snap = new ol.interaction.Snap({source: drawSource});
  map.addInteraction(snap);
}

我正在通过超链接调用 addInteractions() 和 removeInteractions()。在调用 addInteractions() 或调用 removeInteractions() 之前没有出现错误。

【问题讨论】:

    标签: javascript openlayers-5


    【解决方案1】:

    绘制类型应该是字符串

      draw = new ol.interaction.Draw({
        source: drawSource,
        type: 'Polygon'
      });
    

    【讨论】:

    • 更好的是,使用 ol/geom/GeometryType 中的 GeometryType.POLYGON
    • @Snote 仅可作为导入使用,它不包含在 ol.js 旧版构建中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-04
    • 1970-01-01
    相关资源
    最近更新 更多