【问题标题】:openlayers get latest feature added on vectorlayeropenlayers 在矢量图层上添加了最新功能
【发布时间】:2013-08-12 09:25:58
【问题描述】:

我尝试在将特征添加到矢量图层后直接获取最新的特征 ID(例如“OpenLayers_Feature_Vector_86”)。

我尝试添加一个事件监听器:

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

使用以下代码添加该功能:

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

提前致谢 弗洛里安

【问题讨论】:

    标签: vector openlayers layer


    【解决方案1】:

    featuresadded(注意复数形式)传递一个完整的 array 添加的功能(即使它是一个单独的功能,它仍然包装在一个数组中),在您的示例中,您尝试访问特征数组的fid 属性,返回undefined。您应该:

    1. 监听featureadded事件(单数形式):

      传递给侦听器的事件对象将具有一个特性属性,其中引用了添加的特性。

    2. 提取featuresadded处理程序中的相关特征,即:
    featuresadded: function(features) {
      var lastFeature = features[features.length - 1];
      var lastFeatureId = lastFeature.id;
    }
    

    作为一般提示:我建议使用 console.log 而不是 alert 进行调试,它显示对象的所有属性,使您能够检查它(在这种情况下:请参阅 ID 属性被调用id,而不是 fid)。 Alert 将对象强制转换为字符串,通常显示一些无用的内容,例如 [object Object]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多