angular-leaflet-directive icon indicating copy to clipboard operation
angular-leaflet-directive copied to clipboard

Add custom Control

Open Diolor opened this issue 13 years ago • 9 comments

If I see correctly the .js main file I declare custom controls like that:

...,
customControls : [
                L.mapbox.geocoderControl('foursquare.map-ikj05elx'),
                L.control.locate({
                                    setView: true, 
                                    locateOptions: {maxZoom: 10}
                })
],...

Which I would traditionally declare like:

 map.addControl( L.mapbox.geocoderControl('foursquare.map-ikj05elx') )
       .addControl( L.control.locate({
                                    setView: true, 
                                    locateOptions: {maxZoom: 10} })
        )

However it doesn't work. Do I do something wrong?

Diolor avatar Oct 15 '13 18:10 Diolor

have a look at pull request https://github.com/tombatossals/angular-leaflet-directive/pull/114

controller

$scope.controls = {
    custom: []
};
var MyControl = L.control();
    MyControl.setPosition('bottomleft');
    MyControl.onAdd = function () {
        var className = 'leaflet-control-my-location', 
             container = L.DomUtil.create('div', className + ' leaflet-bar');
        return container;
    }

 map.controls.custom.push(MyControl);

html

<leaflet controls="controls"></leaflet>

mchambaud avatar Jan 24 '14 18:01 mchambaud

For anyone who's interested, a 3rd-party control such as leaflet-locatecontrol can be added like so:

Controller

$scope.controls = {
    custom: [
        L.control.locate({ follow: true })
    ]
};

HTML

<leaflet controls="controls"></leaflet>

porjo avatar Feb 18 '14 01:02 porjo

Hey @porjo I'm trying to add leaflet-locatecontrol as you suggested but I get this error:

TypeError: Cannot read property 'custom' of undefined

Any idea what I'm doing wrong?

HTML

<leaflet defaults="map.defaults" 
         controls="controls" 
         center="map.center" 
         markers="markers">
</leaflet>

Controller

  $scope.map = {
    defaults: {
      maxZoom: 18,
      minZoom: 11
    },
    center: {
      lat: 40.055048,
      lng: 17.976894,
      zoom: 15
    }
  };

  $scope.markers = {};

  $scope.controls = {
    custom: []
  };

  leafletData.getMap().then(function(map) {
    map.controls.custom.push(L.control.locate({
      follow: true
    }));
  });

tonyrh avatar May 26 '14 11:05 tonyrh

My solution to get the sidebar control working was to change the angular-leaflet-directive code itself: see #362 and https://github.com/tombatossals/angular-leaflet-directive/blob/master/src/directives/controls.js#L18

wilhelmberg avatar May 26 '14 11:05 wilhelmberg

@tonyrh sorry my code was wrong. I've updated the post.

porjo avatar May 26 '14 22:05 porjo

@BergWerkGIS Would you please elaborate a little on your solution? Although adapting the controls directive I can't get the sidebar to work properly. Thanks in advance

stefan2k avatar Dec 21 '14 14:12 stefan2k

Old issue, code has changed a lot since this, closing. Please reply if you think this should be reopened. Thanks.

tombatossals avatar May 14 '15 16:05 tombatossals

This should be reopened https://github.com/tombatossals/angular-leaflet-directive/issues/929

nzmedet avatar Sep 09 '15 10:09 nzmedet

I worked this out by commenting the following lines. if (!isValidControlType(controlType)) { $log.error(errorHeader + ' Invalid control type: ' + controlType + '.'); return; }

OR may be just add "custom" in controlTypes array around line 284.

itskashi avatar Nov 02 '15 10:11 itskashi