This guide will help you get up and running with ol-contextmenu in your OpenLayers project.
- OpenLayers: Version 7.0.0 or higher
- Modern Browser: Chrome, Firefox, Safari, or Edge with ES6 support
- Build Tool (optional): Webpack, Vite, Rollup, or similar
npm install ol-contextmenuIf you don't have OpenLayers installed:
npm install ol ol-contextmenuFor quick prototyping or simple projects:
<script src="https://cdn.jsdelivr.net/npm/ol@latest/dist/ol.js"></script>
<script src="https://cdn.jsdelivr.net/npm/ol-contextmenu"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@latest/ol.css"><script src="https://unpkg.com/ol@latest/dist/ol.js"></script>
<script src="https://unpkg.com/ol-contextmenu"></script>
<link rel="stylesheet" href="https://unpkg.com/ol@latest/ol.css">ES6 Modules:
import Map from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import OSM from 'ol/source/OSM';
import ContextMenu from 'ol-contextmenu';CommonJS:
const ContextMenu = require('ol-contextmenu');CDN (Global):
// Available as global: ContextMenu
const contextmenu = new ContextMenu();const map = new Map({
target: 'map',
layers: [
new TileLayer({
source: new OSM(),
}),
],
view: new View({
center: [0, 0],
zoom: 2,
}),
});const contextmenu = new ContextMenu({
width: 170,
defaultItems: true,
items: [
{
text: 'Center map here',
callback: (obj) => {
map.getView().animate({
center: obj.coordinate,
duration: 700,
});
},
},
'-', // Separator
{
text: 'Some Action',
callback: () => {
alert('Action clicked!');
},
},
],
});map.addControl(contextmenu);Here's a complete working example:
<!DOCTYPE html>
<html>
<head>
<title>ol-contextmenu Example</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@latest/ol.css">
<style>
#map {
width: 100%;
height: 600px;
}
</style>
</head>
<body>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@latest/dist/ol.js"></script>
<script src="https://cdn.jsdelivr.net/npm/ol-contextmenu"></script>
<script>
const map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: [0, 0],
zoom: 2
})
});
const contextmenu = new ContextMenu({
width: 170,
items: [
{
text: 'Center map here',
callback: function(obj) {
map.getView().animate({
center: obj.coordinate,
duration: 700
});
}
}
]
});
map.addControl(contextmenu);
</script>
</body>
</html>- API Reference - Learn about all available methods and options
- Examples & Recipes - See common patterns and use cases
- TypeScript Guide - Add type safety to your project
{
text: 'Add Marker',
icon: 'path/to/icon.png', // URL or data URI
callback: addMarker,
}{
text: 'Actions',
items: [
{
text: 'Action 1',
callback: action1,
},
{
text: 'Action 2',
callback: action2,
},
],
}const items = [
{ text: 'Item 1', callback: fn1 },
'-', // Separator
{ text: 'Item 2', callback: fn2 },
];Menu doesn't appear: Make sure you've added the control to the map with map.addControl(contextmenu).
Styles look wrong: The CSS is bundled automatically since v6.0. No need to import separately.
TypeScript errors: Make sure you're using the correct import syntax (see TypeScript Guide).
For more help, check the Troubleshooting Guide.