Context menu

by Vladimir Vershinin

HTML

<script src="https://unpkg.com/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io/en/v5.3.0/build/ol.js"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/lib/style.css">
<script src="https://unpkg.com/[email protected]/lib/index.umd.js"></script>
<script src="https://unpkg.com/[email protected]/dist/ol-contextmenu.js"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/ol-contextmenu.min.css">
<div id="app">
  <vl-map ref="map" @created="onMapCreated">
    <vl-view :zoom.sync="zoom" :center.sync="center"></vl-view>
    
    <vl-layer-tile>
      <vl-source-osm />
    </vl-layer-tile>
  </vl-map>  
</div>

CSS

html, body, #app {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

JavaScript

new Vue({
	el: '#app',
  data () {
  	return {
    	zoom: 2,
      center: [0, 0],
    }
  },
  methods: {
  	/**
     * @var {Vue} vl-map instance
     */
  	onMapCreated (map) {
    	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: (...args) => console.log(args), // `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: (...args) => console.log(args),
          },
          '-', // this is a separator
        ],
      })
      
      map.$map.addControl(contextmenu)
    },
  },
})