OpenLayers Custom Context Menu
A contextmenu
extension for OpenLayers. Requires OpenLayers v3.11.0 or higher.
Demo
You can see here a demo or JSFiddle.
How to use it?
jsDelivr
CDN Hosted -Load CSS and Javascript:
<link href="//cdn.jsdelivr.net/openlayers.contextmenu/latest/ol3-contextmenu.min.css" rel="stylesheet"><script src="//cdn.jsdelivr.net/openlayers.contextmenu/latest/ol3-contextmenu.js"></script>
CDN Hosted - UNPKG
Load CSS and Javascript:
<link href="https://unpkg.com/ol3-contextmenu/build/ol3-contextmenu.min.css" rel="stylesheet"><script src="https://unpkg.com/ol3-contextmenu/build/ol3-contextmenu.js"></script>
Self hosted
Download latest release and (obviously) load CSS and Javascript.
Instantiate with some options and add the Control
var 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;
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.
var 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?
var { vectorLayer;};var removeMarkerItem = text: 'Remove this Marker' icon: 'img/marker.png' callback: removeMarker; var restore = false;contextmenu;
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 enabledwidth
:150
; The menu's widthitems
:[]
; An array of object|string
Methods
contextmenu.clear()
Remove all elements from the menu.
contextmenu.close()
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:
var contextmenu = ;map; var add_later = '-' // this is a separator text: 'Add a Marker' icon: 'img/marker.png' callback: marker ;contextmenu;
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
beforeopen
If you want to disable this plugin under certain circumstances, listen to contextmenu;
Listen and make some changes when context menu opens
contextmenu;
Any action when context menu gets closed?
contextmenu;