簡體   English   中英

openlayers獲得在vectorlayer上添加的最新功能

[英]openlayers get latest feature added on vectorlayer

在將功能添加到矢量圖層后,我嘗試直接獲取最新的功能ID(例如,“ OpenLayers_Feature_Vector_86”)。

我試圖添加一個事件監聽器:

eventListeners: {
 "featuresadded": function(feature) {
   alert(feature.fid);
  }
}

該功能添加了以下代碼:

vectors.addFeatures(geojson_format.read(featurecollection));

在此先感謝Florian

featuresadded (請注意復數形式)傳遞添加的整個功能數組 (即使它是單個功能,它仍包裹在數組中),在您的示例中,您嘗試訪問功能數組的fid屬性,該屬性返回undefined 您應該:

  1. 偵聽功能featureadded事件(單數形式):

    傳遞給偵聽器的事件對象將具有一個feature屬性,其中包含對所添加功能的引用。

  2. 將相關特征提取到featuresadded處理程序中,即:

featuresadded: function(features) {
  var lastFeature = features[features.length - 1];
  var lastFeatureId = lastFeature.id;
}

作為一般提示:我建議使用console.log代替alert進行調試,它會顯示對象的所有屬性,使您可以對其進行檢查(在這種情況下:請參閱ID屬性,稱為id ,而不是fid ) 。 Alert將對象強制為字符串,通常顯示無用的內容,例如[object Object]

暫無
暫無

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

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