Openlayers 3 地图上下文菜单
Openlayers 3 map contextmenu
我想要一个右键单击上下文菜单,其中包含单击点的信息。
即我右键单击地图,得到一个下拉菜单,如果我要选择 'add marker' 或类似的,我需要有点击的位置。
我认为获得正确版本的最简单方法是,如果有人可以在右键单击时添加一个简单的下拉菜单到此 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
我有一个解决方案,我从 <div>
元素创建自己的上下文菜单并将其定位到鼠标。菜单项定义onclick,并给出位置,所以我可以放置标记,可以开始绘制区域...
这是一个优雅的解决方案吗?有更好的方法吗?
// ...
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') {
//...
}
}
更新:
现在您可以收听一些(暂时是两个)事件。例如,如果您想在 菜单打开之前设置一些条件并更改菜单项 :
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 the first version of a Custom Context Menu extension for Openlayers 3. It is like that 传单。它是一个 ol.control.Control
扩展,因此您可以将其添加到地图中:
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。
欢迎投稿。
对于 2020 年浏览这些页面的任何人,OpenLayers 现在有一个 map.on('contextmenu', function)
事件处理程序
我想要一个右键单击上下文菜单,其中包含单击点的信息。
即我右键单击地图,得到一个下拉菜单,如果我要选择 'add marker' 或类似的,我需要有点击的位置。
我认为获得正确版本的最简单方法是,如果有人可以在右键单击时添加一个简单的下拉菜单到此 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
我有一个解决方案,我从 <div>
元素创建自己的上下文菜单并将其定位到鼠标。菜单项定义onclick,并给出位置,所以我可以放置标记,可以开始绘制区域...
这是一个优雅的解决方案吗?有更好的方法吗?
// ...
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') {
//...
}
}
更新:
现在您可以收听一些(暂时是两个)事件。例如,如果您想在 菜单打开之前设置一些条件并更改菜单项 :
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 the first version of a Custom Context Menu extension for Openlayers 3. It is like that 传单。它是一个 ol.control.Control
扩展,因此您可以将其添加到地图中:
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。 欢迎投稿。
对于 2020 年浏览这些页面的任何人,OpenLayers 现在有一个 map.on('contextmenu', function)
事件处理程序