簡體   English   中英

QGIS 2 web openlayers - 類型錯誤:無法讀取 null 的屬性

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

我想在 QGIS2web 插件生成的 OpenLayers map 中繪制特征。 由於一切似乎都很好,並且現在所有功能都出來了, 與最近的情況不同

我的完整代碼在這里:

https://jsfiddle.net/go39r4j7/

但我對這種情況還不夠確信,因為我不斷收到錯誤消息:

TypeError:無法讀取 null 的屬性(讀取“獲取”)

在此處輸入圖像描述

它指出了以下代碼:

  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;
              }
          }

從理論上講,我知道,我無法訪問 object,即 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

功能回調。 回調將使用兩個 arguments 調用。 第一個參數是像素處的一個特征或渲染特征,第二個是特征的層,對於非托管層將為 null。

如果您有一個非托管層(例如 Draw 交互使用的覆蓋層),您需要確保在使用.get之前有一個層:

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

我以這種方式解決了它:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM