# Getting Started with ol-contextmenu This guide will help you get up and running with `ol-contextmenu` in your OpenLayers project. ## Prerequisites - **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 ## Installation ### npm (Recommended) ```bash npm install ol-contextmenu ``` If you don't have OpenLayers installed: ```bash npm install ol ol-contextmenu ``` ### CDN For quick prototyping or simple projects: #### jsDelivr ```html ``` #### UNPKG ```html ``` ## Basic Setup ### 1. Import the Library **ES6 Modules:** ```javascript 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:** ```javascript const ContextMenu = require('ol-contextmenu'); ``` **CDN (Global):** ```javascript // Available as global: ContextMenu const contextmenu = new ContextMenu(); ``` ### 2. Create Your Map ```javascript const map = new Map({ target: 'map', layers: [ new TileLayer({ source: new OSM(), }), ], view: new View({ center: [0, 0], zoom: 2, }), }); ``` ### 3. Initialize Context Menu ```javascript 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!'); }, }, ], }); ``` ### 4. Add to Map ```javascript map.addControl(contextmenu); ``` ## Complete Example Here's a complete working example: ```html