I would like to have a right-click context menu with the information of the point clicked.
I.e. I right click on map, get a dropdown menu, here if I would pick 'add marker' or similar, I need to have the clicked position.
I think simplest for getting the version right would be, if someone could add a simple drpdown menu on rightclick to this Test Fiddle
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: 'map',
controls: ol.control.defaults({
attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
collapsible: false
})
}),
view: new ol.View({
center: [0, 0],
zoom: 2
})
});
I've seen this solution, but it didn't work out:
I would like to have a right-click context menu with the information of the point clicked.
I.e. I right click on map, get a dropdown menu, here if I would pick 'add marker' or similar, I need to have the clicked position.
I think simplest for getting the version right would be, if someone could add a simple drpdown menu on rightclick to this Test Fiddle
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: 'map',
controls: ol.control.defaults({
attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
collapsible: false
})
}),
view: new ol.View({
center: [0, 0],
zoom: 2
})
});
I've seen this solution, but it didn't work out: https://gis.stackexchange./questions/148428/how-can-i-select-a-feature-in-openlayers-3-by-right-click-it
Share Improve this question edited Apr 13, 2017 at 12:33 CommunityBot 11 silver badge asked Aug 26, 2015 at 3:58 DDanDDan 8,2766 gold badges38 silver badges63 bronze badges 2- 1 Just bine that solution with this! – Jonatas Walker Commented Aug 26, 2015 at 10:01
- Thanks for the pointer – DDan Commented Aug 27, 2015 at 3:47
3 Answers
Reset to default 8UPDATE:
Now you can listen to some (two, for now) events. For example, if you want to make some conditionals and change menu items before the menu opens:
contextmenu.on('open', function(evt){
var feature = map.forEachFeatureAtPixel(evt.pixel, function(ft, l){
return ft;
});
// there's a feature at this pixel and I want to add
// an option to remove this feature (marker)
if (feature && feature.get('type') == 'removable') {
// remove all items
contextmenu.clear();
// removeMarkerItem {Array}
// propagate custom data to your callback
removeMarkerItem.data = {
marker: feature
};
contextmenu.push(removeMarkerItem);
} else {
contextmenu.clear();
contextmenu.extend(contextmenu_items);
contextmenu.extend(contextmenu.getDefaultItems());
}
});
http://jsfiddle/jonataswalker/ooxs1w5d/
I just released the first version of a Custom Context Menu extension for Openlayers 3. It is like that for Leaflet. It is a ol.control.Control
extended, so you add it to the map like:
var contextmenu = new ContextMenu();
map.addControl(contextmenu);
If you want some more items (there are some defaults):
var contextmenu = new ContextMenu({
width: 170,
default_items: true,
items: [
{
text: 'Center map here',
callback: center
},
{
text: 'Add a Marker',
icon: 'img/marker.png',
callback: marker
},
'-' // this is a separator
]
});
map.addControl(contextmenu);
Demo Fiddle. Contributions are wele.
I have a solution, where I create own context menu from <div>
elements and position it to the mouse. The menuitems define onclick, and give position, so I can place marker, can start drawing area...
Is this an elegant solution? Is there a better way to do?
Updated Demo
// ...
map.getViewport().addEventListener('contextmenu', function (e) {
e.preventDefault();
openContextMenu(e.layerX, e.layerY);
});
function openContextMenu(x, y) {
$('.contextMenu').remove();
$('body').append('<div class="contextMenu" style=" top: ' + y + 'px; left:' + x + 'px;">' +
'<div class="menuItem" onclick="handleContexMenuEvent(\'addMarker\', \'' + x + '\', \'' + y + '\');"> Add Marker </div>' +
'<div class="menuItem" onclick="handleContexMenuEvent(\'addArea\', \'' + x + '\', \'' + y + '\');"> Add Area </div>' +
'</div>');
}
function handleContexMenuEvent(option, x, y) {
$('.contextMenu').remove();
var location = map.getCoordinateFromPixel([x, y]);
if (option == 'addMarker') {
var feature = new ol.Feature(
new ol.geom.Point(location));
feature.setStyle(iconStyle);
vectorSource.addFeature(feature);
} else if (option == 'addArea') {
//...
}
}
Updated Demo
For anyone trawling through these pages in 2020, OpenLayers now has a map.on('contextmenu', function)
event handler