0

I have two vector layers showed but want to differentiate features show for layer1 and layer2. I have this code:

var feature = map.forEachFeatureAtPixel(evt.pixel,
    function(feature, layer) {
        return feature;
    });
if (feature) {
    var geometry = feature.getGeometry();
    var coord = geometry.getCoordinates();

How can I now add another if clause, something like:

    if (feature.layer == 'layer1') { ...
1

Try something like

var featureLayer;
var feature = map.forEachFeatureAtPixel(evt.pixel,
    function(feature, layer) {
        featureLayer = layer;
        return feature;
    });

if (feature) {
    var geometry = feature.getGeometry();
    var coord = geometry.getCoordinates();
    if (featureLayer.get('title') == 'layer1') {
        ...
    }
}
  • I try but don't work – damjang May 5 at 15:54
  • I've updated the answer. featureLayer.title wouldn't work. It should have been featureLayer.get('title') or featureLayer.get('name') or whatever property you are using to identify your layers. – Mike May 5 at 19:52
  • Yesss, thank you a lot! Now work ok. featureLayer.get('title') work ok, but featureLayer.get('name') not. I search to find if this is the correct property name, but didn't find any useful info. – damjang May 5 at 21:28

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.