如何区分Openlayers3中的特征

How to distinguish the features in Openlayers3

最近在开发一个贴图工具,选择openlayers3作为前端贴图APi。 在我的地图图层中,它有几何图形和图像,我想添加一个功能,当我点击不同类型的功能时,它会执行不同的操作。

在代码中,我需要识别它来自图像或几何。感谢你们的帮助。

你的选择不错。有几种方法可以做到这一点。您可以在功能中存储 属性 并检查它,例如:

map.on('click', function(evt) {
  var feature = map.forEachFeatureAtPixel(evt.pixel,
    function(ft, layer) { return ft; }
  );
  // here I'm using feature.get('type') but can be any name
  if (feature && feature.get('type') == 'some_value') {
    // now you have the clicked feature
  }
});

请注意,所有特征 (ol.Feature) 都有一个几何图形。