Monday 25 October 2021

Openlayers delete element option by mouse rightclick

I would like to enable the delete option after right-clicking on my object. So far, the code I provided doesn't work, because the browser is checking some devices for me, as you can see below:

enter image description here

My code looks like this:

 var polygonInteraction = new ol.interaction.Draw({
 type: 'Polygon',
 source: vectorLayer.getSource()
 });
  polygonInteraction.setActive(false);
  polygonInteraction.on('drawend', onDrawend);
  polygonInteraction.on('drawend', function(e) {
  var title = prompt("Please provide the name", "default");
  var value = prompt("Please provide the value", "undefinied");
  var id = x++
  e.feature.setProperties({
  'Id': id,
  'Name': title,
  'Value': value,
 });
 
 function Rightcl(e) {
  var rightclick;
  if (e) var e = window.event;
  if (e.which) rightclick = (e.which == 3);
  else if (e.button) rightclick = (e.button == 2);
  alert('Delete me' + rightclick); // true or false
 }

I would like to have an option to delete the selected object after the right-click. How can I do it?

UPDATE:

I found a quite good option for achieving it:

https://gis.stackexchange.com/questions/148428/how-can-i-select-a-feature-in-openlayers-3-by-right-click-it

which led me to conclusion like this:

 console.info('contextmenu');
  var feature = map.forEachFeatureAtPixel(map.getEventPixel(e),
   function (feature, layer) {
       return feature;
    });
  
   if (feature) {
   selectInteraction.getFeatures()
   }
   return removeFeature
   });


   var removeFeature = {
   text: 'Remove this object',
   classname: 'marker',
   callback: selectInteraction.getFeatures(),
   }

based also at the solution here:

https://rawgit.com/jonataswalker/ol-contextmenu/master/examples/contextmenu.js

but so far I do the right click and the console shows the "contextmenu" only for me.

My full JSfiddle is here:

https://jsfiddle.net/1x3nuspj/

What I am doing wrong here?

Thanks & Regards



from Openlayers delete element option by mouse rightclick

No comments:

Post a Comment