QGIS 2 web openlayers - TypeError: Cannot read properties of null

QGIS 2 web openlayers - TypeError: Cannot read properties of null

我想在QGIS2web插件生成的OpenLayers地图中绘制要素。由于一切似乎都很好,所有功能现在都出来了,unlike the recent situation

我的完整代码在这里:

https://jsfiddle.net/go39r4j7/

但我对这种情况还不够确信,因为我不断收到错误消息:

TypeError: 无法读取 null 的属性(读取 'get')

其中指出了以下一段代码:

  map.forEachFeatureAtPixel(pixel, function(feature, layer) {
    if (feature instanceof ol.Feature && (layer.get("interactive") || layer.get("interactive") 
   == undefined)) {
        var doPopup = false;
        for (k in layer.get('fieldImages')) {
            if (layer.get('fieldImages')[k] != "Hidden") {
                doPopup = true;
              }
          }

理论上,我知道,我无法访问该对象,该对象为 null:

https://idiallo.com/javascript/uncaught-typeerror-cannot-read-property-of-null

但是我尝试过的任何选项(删除这段代码,删除 layer.get 属性,将 layer.get("interactive") 更改为我自己的 id ("draw"))没成功。

此外,这段代码似乎是类似情况的标准代码:

https://gist.github.com/riccardoklinger/e3e2c512a366c8a6a296c10eef9c4162

我该如何解决这个问题?该工具有效,尽管我担心未来的修改。

https://openlayers.org/en/latest/apidoc/module-ol_Map-Map.html#forEachFeatureAtPixel

Feature callback. The callback will be called with two arguments. The first argument is one feature or render feature at the pixel, the second is the layer of the feature and will be null for unmanaged layers.

如果您有一个非托管层(例如 Draw 交互使用的覆盖层),您需要确保在使用 .get 之前有一个层:

if (feature instanceof ol.Feature && layer && (layer.get("interactive") || layer.get("interactive") == undefined)) {