As angular-leaflet-directive integrates with Leaflet.draw?
I have a code like this: http://tombatossals.github.io/angular-leaflet-directive/#!/examples/geojson
I would like to have this: // Initialise the FeatureGroup to store editable layers var drawnItems = new L.FeatureGroup(); map.addLayer(drawnItems);
// Initialise the draw control and pass it the FeatureGroup of editable layers var drawControl = new L.Control.Draw({ edit: { featureGroup: drawnItems } }); map.addControl(drawControl);
Hi!
I found a way to integrate Draw Control with this directive.
In mapController code.
var drawnItems = new L.FeatureGroup();
var options = {
position : 'topright',
draw : {
circle : false
},
edit: {
featureGroup: drawnItems
},
remove : false
};
var drawControl = new L.Control.Draw(options);
this.controls : {
custom: [drawControl]
}
Then in the markup:
<div ng-controller="mapController as mapCtrl">
<leaflet controls="mapCtrl.controls"></leaflet>
</div>
I hope this peace of code will help you.
The better way is:
var app = angular.module("demoapp", ["leaflet-directive"]);
app.controller("DemoController", [ "$scope", "leafletData", function($scope, leafletData) {
angular.extend($scope, {
london: {
lat: 51.505,
lng: -0.09,
zoom: 4
},
controls: {
draw: {}
}
});
leafletData.getMap().then(function(map) {
var drawnItems = $scope.controls.draw.edit.featureGroup;
map.on('draw:created', function (e) {
var layer = e.layer;
drawnItems.addLayer(layer);
console.log(JSON.stringify(layer.toGeoJSON()));
});
});
}]);
HTML:
<body ng-controller="DemoController">
<h1>Draw control example</h1>
<p>Draw a shape and a geoJSON data structure will be shown on the console.log.</p>
<leaflet center="london" controls="controls" width="640" height="400"></leaflet>
</body>
With the "better way", I got the following error: TypeError: Cannot read property 'featureGroup' of undefined
No extra code added. Any idea ?
I also received that same error
I think this works: Some brackets might be missing though
var app = angular.module("demoapp", ["leaflet-directive"]); app.controller("DemoController", [ "$scope", "leafletData", function($scope, leafletData) { angular.extend($scope, { london: { lat: 51.505, lng: -0.09, zoom: 4 }, controls: { } });
leafletData.getMap().then(function(map) {
var drawnItems = new L.featureGroup().addTo(map);
map.addControl(new L.Control.Draw({
edit: {featureGroup: drawnItems }
}));
map.on('draw:created', function (event) {
var layer = event.layer;
drawnItems.addLayer(layer);
console.log(JSON.stringify(layer.toGeoJSON()));
});
});
Hi, I have some trouble with leaflet draw. Everything is good with @elesdoar method until i finished to draw my line, every mouse movement trigger an error. Uncaught TypeError: Cannot read property 'updatePosition' of nullle leaflet.draw.js:9 L.Draw.Polyline.L.Draw.Feature.extend._onMouseMove leaflet.js:6 o.Mixin.Events.fireEvent leaflet.js:6 o.Map.o.Class.extend._fireMouseEvent leaflet.js:8 o.DomEvent.addListener.t.(anonymous function).s
And when I zoom in or out the line grow,reduce and move while doing that. WIth this error : Uncaught TypeError: Cannot read property 'length' of undefined leaflet.draw.js:9 L.Draw.Polyline.L.Draw.Feature.extend._updateGuide leaflet.draw.js:9 L.Draw.Polyline.L.Draw.Feature.extend._onZoomEnd leaflet.js:6 o.Mixin.Events.fireEvent leaflet.js:6 o.Map.o.Class.extend._resetView leaflet.js:9 o.Map.include.o.DomUtil.TRANSITION._onZoomTransitionEnd leaflet.js:9 o.Map.include.o.DomUtil.TRANSITION._catchTransitionEnd leaflet.js:8 o.DomEvent.addListener.t.(anonymous function).s Any idea ? Thanks for your help !
Like the others in this issue, I could not get @elesdoar's example working. Poking around, it looks like there is a pretty good example of integrating with leaflet-draw on the community examples page. With that as a guide, I was able to get something similar working without having to resort to using the global L.Control.Draw.
The API has changed since I suggested this example, the correct way is the viewer's example.
@skray Can you provide working code based on the community example? It does not include edit functionality.
@elesdoar What is the "viewer's example" you are referring to?
Coming back to this closed issue, it is very difficult to figure out what the solution is, even though it seems like people have gotten it to work.
@rawbeans, it is here:
http://tombatossals.github.io/angular-leaflet-directive/examples/0000-viewer.html#/controls/draw-example
Basically is the same that @skray is refering.
Yes but that example does not include editing. How do you enable editing?
controls: {
draw: {
edit: ...
}
},
Where edit have leaflet draw edit options: https://github.com/Leaflet/Leaflet.draw#editoptions
Thank you. I did not realize you have nest the draw: and edit: within a top level draw:
controls: {
draw: {
draw: {},
edit: ...
}
},
vs
controls: {
draw: {},
edit: ...
},
What should be in the edit property. The link https://github.com/Leaflet/Leaflet.draw#editoptions seems to be broken, it just leads to the repo home. In the doc for Control.Draw EditOption is mentioned but have default value of false. It is strange..
Is it possible that someone will enable editing feature in the example ? http://tombatossals.github.io/angular-leaflet-directive/examples/0000-viewer.html#/controls/draw-example
baselayers.overlays.draw from the draw-example does not return L.FeatureGroup object which is required by the edit: { featureGroup: ... }. I don't see other way of achieving that.
@rawbeans point should be 'more documented', maybe in the example - took me some time to find that, as it is not very straightforward ;)