获取所选功能的 ID

Get id of feature selected

如何获取选中或取消选中的特征的id?每次选择或取消选择功能时,我都需要调用传递功能的特定函数或它的 id。大致如下:

selectInteraction = new ol.interaction.Select( {
  layers: layers
} );

map.getInteractions().extend( [ selectInteraction ] );

selectInteraction.on( "select", function ( evt ) {

  if( evt.selected.length > 0 ) {
    onFeatureSelect( evt.selected[ 0 ] );
  } else {
    onFeatureUnselect( evt.deselected[ 0 ] );
  }
});

你快到了:

selectInteraction.on('select', function(evt){
    if(evt.selected.length > 0){
        console.info('selected: ' + evt.selected[0].getId());

    }
});