1

How can I add an event on OpenLayers Feature using Feature on method? It doesn't work for me.

import { Map, View, Feature } from 'ol';
import {getVectorContext} from 'ol/render';
import TileLayer from 'ol/layer/Tile';
import * as layer from 'ol/layer';
import VectorImage from 'ol/layer/VectorImage';
import * as source from 'ol/source';
import * as proj from 'ol/proj';
import * as geom from 'ol/geom';
import * as style from 'ol/style';
import EventType from 'ol/events/EventType';

const marker = new Feature({
        geometry: new geom.Point(
          proj.fromLonLat(lonLat)
        )
     });
// marker.setStyle(this.getIconStyle()); 
marker.on(EventType.CLICK, function() {
   alert('click');
});
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Rabindra
  • 11
  • 2

1 Answers1

0

Features don't have a click event, you need to listen for clicks on the map and check if it was on your marker

map.on('click', function(evt) {
  if (map.forEachFeatureAtPixel(evt.pixel,
    function(feature) {
      return feature === marker;
    })
  ) {
    alert('click');
  }
});
Mike
  • 16,042
  • 2
  • 14
  • 30