select 在 openlayers3 中获取坐标的事件

select event for getting coordinate in openlayers3

我希望您使用 select 交互来添加功能,但 e.coordinate 显示未定义

var select = new ol.interaction.Select({
  style: new ol.style.Style({
    stroke: new ol.style.Stroke({
      color: '#0288D1',
      width: 2
    })
  })
});
map.addInteraction(select);

select.on('select', function(e) {

  var feat = new ol.Feature({
    geometry: new ol.geom.Point(e.coordinate),
    style: style1
  });
  alert(e.coordinate);
  feat.setStyle(style1);

  layerVector.getSource().addFeature(feat);
});

如果有人知道原因,请告诉我如何通过此 select 交互点击查看器时获取坐标。

更新

如果您成为 API Docs 的朋友,您在评论中提出的问题(关于听众)会更容易 。在我开始的时候,很难全部了解,但文档要好得多,所以让它成为你的来源。

OpenLayers 的每个部分都有自己的侦听器,例如,单击 ol.Map, scroll to the "Fires:" section and you'll see the several listeners, the same with ol.View 等等。


要在 ol.interaction.Select 侦听器中获取点击坐标,请使用:

select.on('select', function(evt){
    var coord = evt.mapBrowserEvent.coordinate;
    console.info(coord);
    // ...
});