jonataswalker/ol-contextmenu
Fork: 84 Star: 219 (更新于 2024-10-31 12:44:12)
license: MIT
Language: TypeScript .
Custom Context Menu for OpenLayers
最后发布版本: 5.5.0 ( 2024-08-07 20:28:24)
OpenLayers Custom Context Menu
A contextmenu
extension for OpenLayers. Requires OpenLayers v7.0.0 or higher.
Demo
How to use it?
NPM
npm install ol-contextmenu
CDN Hosted - jsDelivr
Load CSS and Javascript:
<link href="https://cdn.jsdelivr.net/npm/ol-contextmenu@latest/dist/ol-contextmenu.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/ol-contextmenu"></script>
CDN Hosted - UNPKG
Load CSS and Javascript:
<link href="https://unpkg.com/ol-contextmenu/dist/ol-contextmenu.css" rel="stylesheet">
<script src="https://unpkg.com/ol-contextmenu"></script>
Self hosted
Download latest release and (obviously) load CSS and Javascript.
Instantiate with some options and add the Control
const contextmenu = new ContextMenu({
width: 170,
defaultItems: true, // defaultItems are (for now) Zoom In/Zoom Out
items: [
{
text: 'Center map here',
classname: 'some-style-class', // add some CSS rules
callback: center, // `center` is your callback function
},
{
text: 'Add a Marker',
classname: 'some-style-class', // you can add this icon with a CSS class
// instead of `icon` property (see next line)
icon: 'img/marker.png', // this can be relative or absolute
callback: marker,
},
'-', // this is a separator
],
});
map.addControl(contextmenu);
You can add a (nested) submenu like this:
If you provide items {Array}
a submenu will be created as a child of the current item.
const all_items = [
{
text: 'Some Actions',
items: [
// <== this is a submenu
{
text: 'Action 1',
callback: action,
},
{
text: 'Other action',
callback: action2,
},
],
},
{
text: 'Add a Marker',
icon: 'img/marker.png',
callback: marker,
},
'-', // this is a separator
];
Would you like to propagate custom data to the callback handler?
const removeMarker = function (obj) {
vectorLayer.getSource().removeFeature(obj.data.marker);
};
const removeMarkerItem = {
text: 'Remove this Marker',
icon: 'img/marker.png',
callback: removeMarker,
};
let restore = false;
contextmenu.on('open', function (evt) {
const feature = map.forEachFeatureAtPixel(evt.pixel, function (ft, l) {
return ft;
});
if (feature) {
contextmenu.clear();
removeMarkerItem.data = { marker: feature };
contextmenu.push(removeMarkerItem);
restore = true;
} else if (restore) {
contextmenu.clear();
contextmenu.extend(contextmenu_items);
contextmenu.extend(contextmenu.getDefaultItems());
restore = false;
}
});
API
Constructor
new ContextMenu(options)
options
is an object with the following possible properties:
-
eventType
:contextmenu
; The listening event type (You could use'click'
,'dblclick'
) -
defaultItems
:true
; Whether the default items (which are: Zoom In/Out) are enabled -
width
:150
; The menu's width -
items
:[]
; An array of object|string
Methods
contextmenu.clear()
Remove all elements from the menu.
contextmenu.closeMenu()
Close the menu programmatically.
contextmenu.extend(arr)
@param {Array} arr
Add items to the menu. This pushes each item in the provided array to the end of the menu.
Example:
const contextmenu = new ContextMenu();
map.addControl(contextmenu);
const add_later = [
'-', // this is a separator
{
text: 'Add a Marker',
icon: 'img/marker.png',
callback: marker,
},
];
contextmenu.extend(add_later);
contextmenu.push(item)
@param {Object|String} item
Insert the provided item at the end of the menu.
contextmenu.shift()
Remove the first item of the menu.
contextmenu.pop()
Remove the last item of the menu.
contextmenu.getDefaultItems()
Get an array of default items.
contextmenu.isOpen()
Whether the menu is open.
contextmenu.updatePosition(pixel)
@param {Array} pixel
Update menu's position.
Events
If you want to disable this plugin under certain circumstances, listen to beforeopen
contextmenu.on('beforeopen', function (evt) {
const feature = map.forEachFeatureAtPixel(evt.pixel, function (ft, l) {
return ft;
});
if (feature) {
// open only on features
contextmenu.enable();
} else {
contextmenu.disable();
}
});
Listen and make some changes when context menu opens
contextmenu.on('open', function (evt) {
const feature = map.forEachFeatureAtPixel(evt.pixel, function (ft, l) {
return ft;
});
if (feature) {
// add some other items to the menu
}
});
Any action when context menu gets closed?
contextmenu.on('close', function (evt) {
// it's upon you
});
最近版本更新:(数据更新于 2024-09-05 20:30:00)
2024-08-07 20:28:24 5.5.0
2024-03-08 20:11:23 5.4.0
2023-09-23 00:19:23 5.3.0
2023-04-01 07:30:14 5.2.1
2023-03-14 00:44:59 5.1.2
2023-03-12 02:16:30 5.1.5
2022-11-12 23:46:31 5.1.1
2022-09-24 21:07:06 5.1.0
2022-09-22 22:33:26 5.0.3
2022-09-21 00:59:06 5.0.0
主题(topics):
contextmenu, openlayers
jonataswalker/ol-contextmenu同语言 TypeScript最近更新仓库
2024-11-22 01:36:46 twentyhq/twenty
2024-11-21 22:40:05 LLOneBot/LLOneBot
2024-11-21 20:34:48 CopilotKit/CopilotKit
2024-11-21 16:58:07 labring/FastGPT
2024-11-21 16:14:54 RSSNext/Follow
2024-11-21 16:12:23 x-extends/vxe-table