我想要一个右键单击上下文菜单,其中包含单击点的信息。
即我右键单击 map ,得到一个下拉菜单,在这里如果我要选择“添加标记”或类似的,我需要有点击的位置。
我认为获得正确版本的最简单方法是,如果有人可以在右键单击此 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
})
});
我见过这个解决方案,但没有成功: https://gis.stackexchange.com/questions/148428/how-can-i-select-a-feature-in-openlayers-3-by-right-click-it
最佳答案
更新:
现在您可以收听一些(目前是两个)事件。例如,如果您想在菜单打开之前设置一些条件并更改菜单项:
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.net/jonataswalker/ooxs1w5d/
我只是released Openlayers 3 的第一版自定义上下文菜单扩展。就像that传单。它是 ol.control.Control
的扩展,因此您可以像这样将它添加到 map 中:
var contextmenu = new ContextMenu();
map.addControl(contextmenu);
如果你想要更多的项目(有一些默认值):
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 . 欢迎投稿。
关于javascript - Openlayers 3 map 上下文菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32217753/