7

I try to load geoJson data in Mapbox and edit it with the plugin Leaflet.Draw

Here is an example : fiddle

var featureGroup = L.featureGroup().addTo(map);

var geojson = {
  "type": "FeatureCollection",
  "features": [ ...........  ]
}


L.geoJson(geojson).addTo(featureGroup);

When i click to the edit button, i have an error :

Uncaught TypeError: Cannot read property 'enable' of undefined

Object seems to be editable but i can't modify it.

What is the correct way to add geojson object in mapbox draw layer ?

muzaffar
  • 1,706
  • 1
  • 15
  • 28
manusvs650
  • 241
  • 2
  • 6

3 Answers3

6

I have found the solution :

L.geoJson(geojson, {
  onEachFeature: function (feature, layer) {
    featureGroup.addLayer(layer);
  }
});
manusvs650
  • 241
  • 2
  • 6
  • I actually have the same issue. I'm using Mapbox though. I've tried adding this to my code, but it doesn't seem to do the trick. [Here's my post about it.](http://gis.stackexchange.com/questions/153097/using-mapbox-leafletjs-how-do-i-edit-imported-shapes-from-geojson) If you could take a look at it, I'd greatly appreciate it. It seems to be adding my GeoJSON to layers, but I'm still getting the same error that you got. – Josh Jul 02 '15 at 15:14
  • Actually, that seemed to do the trick aside from the fact that my circles aren't showing up as circles. That will certainly help me move forward though! Thanks! – Josh Jul 02 '15 at 15:21
3

Here is working example using CoffeeScript:

drawnItems = new L.FeatureGroup()
map.addLayer drawnItems

layers = L.geoJson geojson
layers.eachLayer (layer) => drawnItems.addLayer layer
Stepan Kuzmin
  • 1,031
  • 2
  • 11
  • 21
0

I had to do the following to get mine to work (in addition to the above answers):

L.geoJson(geojson, {
  onEachFeature: function (feature, layer) {
    if (layer.getLayers) {
      layer.getLayers().forEach(function (l) {
        featureGroup.addLayer(l);
      })
    } else {
      featureGroup.addLayer(layer);
    }
  }
});

This was for a geojson that was a "Feature" type.

Patch Rick Walsh
  • 447
  • 2
  • 11