2016-08-12 27 views
0

selectInteractionのselectイベントを発生させようとしています。ここで私はこれまで持っているコードです:OpenLayers 3 - SelectInteractionイベントが起動しない

// 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); 

} 

私は機能を選択したアレイにプッシュされたときにselectイベントが発射されていないことを理解しています。それ以外の場合は期待どおりに動作します。では、これをどのように機能させるには?おそらく別のイベントを聞くことはできますか?あなたがイベントに耳を傾けることができ

答えて

0

クリックするか、地図上singleclick、および選択した機能に返される機能をプッシュ:もちろん

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

これを使用すると、クリックするだけで機能を選択したいと仮定し

関連する問題