Search code examples
angularjsgisangular-leaflet-directive

how to create context menu when right clicking on marker on a map using angular-leaflet-directive


I am new to angular-leaflet-directive.

I want to have a context menu when i right click on a marker.

I couldn't find any examples in angular way.

Can anyone please guide to achieve it.

Is that functionality exists in angular-leaflet-directive ?, because I couldn't find anything about in the documentation.


Solution

  • first download required files for angular context menu from this link

    here is the example,

    <!DOCTYPE html>
    <html ng-app="demoapp">
    
    <head>
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
      <script src="http://cdn.leafletjs.com/leaflet-0.7.1/leaflet.js"></script>
      <script src="http://tombatossals.github.io/angular-leaflet-directive/dist/angular-leaflet-directive.min.js"></script>
      <script src="   leaflet.contextmenu-src"></script>
    
      <link rel="stylesheet" href="leaflet.contextmenu.css" />
    
      <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.1/leaflet.css" />
      <script>
        var app = angular.module("demoapp", ["leaflet-directive"]);
        app.controller('MarkersSimpleController', ['$scope', function($scope) {
    
          $scope.showCoordinates = function(e) {
            alert(e.latlng);
          }
    
          var mainMarker = {
            lat: 51,
            lng: 0,
            focus: true,
            draggable: true,
            message: "hey marker",
            contextmenu: true,
            contextmenuWidth: 140,
            contextmenuItems: [{
              text: 'Show coordinates',
              callback: $scope.showCoordinates
            }]
          };
          angular.extend($scope, {
            london: {
              lat: 51.505,
              lng: -0.09,
              zoom: 8
    
            },
            markers: {
              mainMarker: angular.copy(mainMarker)
            },
            position: {
              lat: 51,
              lng: 0
            },
            events: { // or just {} //all events
              markers: {
                enable: ['dragend']
                  //logic: 'emit'
              }
            }
          });
          $scope.$on("leafletDirectiveMarker.dragend", function(event, args) {
            $scope.position.lat = args.model.lat;
            $scope.position.lng = args.model.lng;
          });
        }]);
      </script>
    </head>
    
    <body ng-controller="MarkersSimpleController">
      <!-- <leaflet lf-center="london" markers="markers" height="480px" width="100%"></leaflet> EVENTS WILL STILL FIRE but all will for each directive-->
      <!-- NOTICE events attribute is optional it is more for options in whitelisting (enable), blacklisting (disable), and logic (emit or broadcast)  -->
      <leaflet lf-center="london" event-broadcast="events" markers="markers" height="480px" width="100%"></leaflet>
      <h1>Markers simple example</h1>
      <p>Initial marker position (lat/lng): <strong ng-bind="markers.mainMarker.lat"></strong> / <strong ng-bind="markers.mainMarker.lng"></strong></p>
      <p>Actual marker position (lat/lng): <strong ng-bind="position.lat"></strong> / <strong ng-bind="position.lng"></strong></p>
      </p>
    </body>
    
    </html>
    

    Plunker link for the same