All Projects → jonataswalker → Ol Contextmenu

jonataswalker / Ol Contextmenu

Licence: mit
Custom Context Menu for OpenLayers

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Ol Contextmenu

Gisportal
A web-based GIS tool for visualisation and analysis of geospatial data
Stars: ✭ 48 (-65.96%)
Mutual labels:  openlayers
Ngcontextmenu
Handcraft your very own context menus for a richer UX!
Stars: ✭ 81 (-42.55%)
Mutual labels:  contextmenu
Lizmap Web Client
Transfer a QGIS project on a server, Lizmap is providing the web interface to browse it
Stars: ✭ 130 (-7.8%)
Mutual labels:  openlayers
Xyz
An open source javascript framework for spatial data and application interfaces.
Stars: ✭ 54 (-61.7%)
Mutual labels:  openlayers
Tc Material Design
Série de artigos sobre o Material Design Android
Stars: ✭ 64 (-54.61%)
Mutual labels:  contextmenu
Mangol
Maps created with Angular & OpenLayers using Material design
Stars: ✭ 92 (-34.75%)
Mutual labels:  openlayers
Tegola
Tegola is a Mapbox Vector Tile server written in Go
Stars: ✭ 754 (+434.75%)
Mutual labels:  openlayers
Data Visualization
Data visualization demos, mainly geographic data.
Stars: ✭ 137 (-2.84%)
Mutual labels:  openlayers
React Native Ios Context Menu
A react-native component to use context menu's (UIMenu) on iOS 13/14+
Stars: ✭ 80 (-43.26%)
Mutual labels:  contextmenu
Ngeo
Library combining OpenLayers and AngularJS
Stars: ✭ 122 (-13.48%)
Mutual labels:  openlayers
Gwt Ol
GWT wrapper for OpenLayers 3+ using JSInterop
Stars: ✭ 57 (-59.57%)
Mutual labels:  openlayers
Ol Games
🎮 Game stuff for Openlayers, powered by HTML5, canvas, javascript and Openlayers.
Stars: ✭ 61 (-56.74%)
Mutual labels:  openlayers
React Contextmenu
Project is no longer maintained
Stars: ✭ 1,361 (+865.25%)
Mutual labels:  contextmenu
Openlayers
OpenLayers
Stars: ✭ 8,612 (+6007.8%)
Mutual labels:  openlayers
Ngx Openlayers
Angular2+ components for Openlayers 4.x
Stars: ✭ 131 (-7.09%)
Mutual labels:  openlayers
Geography for hackers
Geography for Hackers - Teaching all how to hack geography, use GIS, and think spatially
Stars: ✭ 25 (-82.27%)
Mutual labels:  openlayers
Webclient Javascript
MapGIS Client for JavaScript, is a cloud GIS network client development platform. It makes a perfect fusion of traditional WebGIS and cloud GIS; also integrates four mainstream map open source frameworks and visualization libraries such as Echarts, MapV, and D3, etc.. Therefore, highly-efficient visual expression and analysis of big data and real-time streaming data have been further enhanced.
Stars: ✭ 88 (-37.59%)
Mutual labels:  openlayers
Openlayers Editor
OpenLayers Editor
Stars: ✭ 138 (-2.13%)
Mutual labels:  openlayers
Vue Contextmenu
contextmenu component for Vue2
Stars: ✭ 134 (-4.96%)
Mutual labels:  contextmenu
Geoext3
A JavaScript framework that combines the GIS functionality of OpenLayers with all features of the ExtJS library
Stars: ✭ 121 (-14.18%)
Mutual labels:  openlayers

OpenLayers Custom Context Menu

build status npm version license dependency status devDependency status

A contextmenu extension for OpenLayers. Requires OpenLayers v3.11.0 or higher.

contextmenu anim

Demo

You can see here a demo or JSFiddle.

How to use it?

NPM

npm install ol-contextmenu

CDN Hosted - jsDelivr

Load CSS and Javascript:

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/ol-contextmenu.min.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.min.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
var 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.

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 removeMarker = function (obj) {
  vectorLayer.getSource().removeFeature(obj.data.marker);
};
var removeMarkerItem = {
  text: 'Remove this Marker',
  icon: 'img/marker.png',
  callback: removeMarker
};

var restore = false;
contextmenu.on('open', function (evt) {
  var 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.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 = new ContextMenu();
map.addControl(contextmenu);

var 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) {
  var 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){
  var 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
});
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].