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:
My code looks like this:
JavaScript
x
24
24
1
var polygonInteraction = new ol.interaction.Draw({
2
type: 'Polygon',
3
source: vectorLayer.getSource()
4
});
5
polygonInteraction.setActive(false);
6
polygonInteraction.on('drawend', onDrawend);
7
polygonInteraction.on('drawend', function(e) {
8
var title = prompt("Please provide the name", "default");
9
var value = prompt("Please provide the value", "undefinied");
10
var id = x++
11
e.feature.setProperties({
12
'Id': id,
13
'Name': title,
14
'Value': value,
15
});
16
17
function Rightcl(e) {
18
var rightclick;
19
if (e) var e = window.event;
20
if (e.which) rightclick = (e.which == 3);
21
else if (e.button) rightclick = (e.button == 2);
22
alert('Delete me' + rightclick); // true or false
23
}
24
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:
which led me to conclusion like this:
JavaScript
1
19
19
1
console.info('contextmenu');
2
var feature = map.forEachFeatureAtPixel(map.getEventPixel(e),
3
function (feature, layer) {
4
return feature;
5
});
6
7
if (feature) {
8
selectInteraction.getFeatures()
9
}
10
return removeFeature
11
});
12
13
14
var removeFeature = {
15
text: 'Remove this object',
16
classname: 'marker',
17
callback: selectInteraction.getFeatures(),
18
}
19
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
Advertisement
Answer
Here is an working example
JavaScript
1
36
36
1
<!doctype html>
2
<html lang="en">
3
<head>
4
<meta charset="utf-8">
5
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.9.0/css/ol.css" type="text/css">
6
<style>
7
.map {
8
height: 400px;
9
width: 100%;
10
}
11
</style>
12
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.9.0/build/ol.js"></script>
13
<link href="https://cdn.jsdelivr.net/npm/ol-contextmenu@latest/dist/ol-contextmenu.min.css" rel="stylesheet">
14
<script src="https://cdn.jsdelivr.net/npm/ol-contextmenu"></script>
15
<title>OpenLayers example</title>
16
</head>
17
<body>
18
<h2>My Map</h2>
19
<div id="map" class="map"></div>
20
<script type="text/javascript">
21
var map = new ol.Map({
22
target: 'map',
23
layers: [
24
new ol.layer.Tile({
25
source: new ol.source.OSM()
26
})
27
],
28
view: new ol.View({
29
center: ol.proj.fromLonLat([4.35247, 50.84673]),
30
zoom: 4
31
})
32
});
33
</script>
34
</body>
35
</html>
36
JavaScript
1
47
47
1
var contextmenu = new ContextMenu({
2
width: 170,
3
defaultItems: true, // defaultItems are (for now) Zoom In/Zoom Out
4
items: []
5
});
6
7
8
9
var removeMarker = function (obj) {
10
layer.getSource().removeFeature(obj.data.marker);
11
};
12
var removeMarkerItem = {
13
text: 'Remove this Marker',
14
icon: 'img/marker.png',
15
callback: removeMarker
16
};
17
18
var restore = false;
19
contextmenu.on('open', function (evt) {
20
var feature = map.forEachFeatureAtPixel(evt.pixel, function (ft, l) {
21
return ft;
22
});
23
if (feature) {
24
contextmenu.clear();
25
removeMarkerItem.data = { marker: feature };
26
contextmenu.push(removeMarkerItem);
27
restore = true;
28
} else if (restore) {
29
contextmenu.clear();
30
contextmenu.extend(contextmenu_items);
31
contextmenu.extend(contextmenu.getDefaultItems());
32
restore = false;
33
}
34
});
35
36
var layer = new ol.layer.Vector({
37
source: new ol.source.Vector({
38
features: [
39
new ol.Feature({
40
geometry: new ol.geom.Point(ol.proj.fromLonLat([4.35247, 50.84673]))
41
})
42
]
43
})
44
});
45
map.addLayer(layer);
46
map.addControl(contextmenu);
47