1

I looked for a way to fire onclick event and I found Adding event handler to feature in OpenLayers 3? at Stack Overflow. I undestand from this question is that there is no event onclick to features. I want to understand why is it? And maybe there is upcoming to the API and they have added a click events.

the way they offered to do it is like that:

map.on("click", function(e) {
    map.forEachFeatureAtPixel(e.pixel, function (feature, layer) {
        //do something
    }
}

It means i need to fire a click on the whole div map, then look for my specificPixel that the feature contains and then do whatever i want. It sounds primitive and will take too long for a high amount of features.

Is there another way to solve this problem?

Secondly, as i see there is no that much events on OpenLayers 3 api . Why is it?

3

It means i need to fire a click on the whole div map, then look for my specificPixel that the feature contains and then do whatever i want.

I don't think you understand how forEachFeatureAtPixel works. Once you click, it takes the pixel clicked and iterate through all the layers to find any feature within a range of that pixel coordinate. It's not going through all the pixels of the map. Look at the code to better understand this. Further, it doesn't even iterate through "ALL" the features, it finds the features which are currently visible in the frame state. Alternatively, if you have too many features, You can divide your features to few layers and apply a layer filter too

There is another way to solve this problem?

Yes, you can add a select interaction to the map and use 'add' event on its feature collection.

var select_interaction = new ol.interaction.Select();

select_interaction.getFeatures().on("add", function (e) { 
     var feature = e.element; //the feature selected
});

map.addInteraction(select_interaction);
10
  • I'm not interested in on "add" event however i interested using the "click" can i use this event in this case , and how can i know which kind of events the api provides ? Secondly there is any built in function to popup feature ? Jan 8 '16 at 7:40
  • 1
    Individual features don't fire click events. So If you don't want to do this with interactions, the events fired from map is the only way I know. Here you can see all the events fired from map.
    – itsyahani
    Jan 8 '16 at 7:47
  • 2
    I don't think you understand how forEachFeatureAtPixel works. Once you click, it takes the pixel clicked and iterate through all the layers to find any feature intersects with the pixel coordinate. It's not going through all the pixels of the map. Look at the code to better understand this
    – itsyahani
    Jan 8 '16 at 9:54
  • 2
    The method we are talking about works pretty fast and if you really look at it, you'll see that it doesn't even iterate through "ALL" the features, it finds the features which are currently visible in the frame state. Alternatively, if you have too many features, You can divide your features to few layers and apply a layer filter too.
    – itsyahani
    Jan 8 '16 at 10:12
  • 1
    @itsyahani your comments should be part of your answer! Jan 8 '16 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.