【问题标题】:OpenLayers 3 - SelectInteraction Event Not FiringOpenLayers 3 - SelectInteraction 事件未触发
【发布时间】:2016-08-12 15:15:56
【问题描述】:

我正在尝试为 selectInteraction 触发 select 事件。这是我到目前为止的代码:

// create and instance of the selectInteraction
var selectInteraction = new ol.interaction.Select( {
    layers: myLayers
} );

// add select event handler
// NOT BEING CALLED WHEN FEATURES ARE PUSHED TO SELECTED ARRAY
selectInteraction.on( "select", function ( evt ) {

    var selected = evt.selected;
    var deselected = evt.deselected;

    selected.forEach( function( feature ) {
        feature.setStyle( myCustomStyleFunction );
    } );

    deselected.forEach( function( feature ) {
        feature.setStyle( null );
    } );


}, selectInteraction );

// add the interaction to the map
myMap.getInteractions().extend( [ selectInteraction ] );

// function called with feature to be selected
function programmaticallySelectFeature( feature ) {

    // get the selectInteraction for the map
    myMap.getInteractions().forEach( function ( interaction ) {

        if ( interaction instanceof ol.interaction.Select ) {
            selectInteraction =  interaction;
        }

    });

    // push the feature to the selectInteraction
    selectInteraction.getFeatures().push( feature );

}

我了解当功能被推送到所选数组时,选择事件不会触发。否则它会按预期工作。那么如何才能让它发挥作用呢?我可以监听另一个事件吗?

【问题讨论】:

    标签: openlayers-3


    【解决方案1】:

    可以在地图上监听click或singleclick事件,并在选中的features中推送返回的feature:

    map.on('click', function(evt){
        var feature = map.forEachFeatureAtPixel(evt.pixel, function(feature, layer) {
            return feature;
        });
        if(feature){
            selectInteraction.getFeatures().push(feature);
        }
     });
    

    这当然是假设您想通过点击选择功能

    【讨论】:

      猜你喜欢
      • 2014-04-25
      • 1970-01-01
      • 2013-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      相关资源
      最近更新 更多