0

I am working on a project which uses Leaflet and leaflet.draw to show and draw shapes on the map. I also have a data.json file which I use to display former features generated from geojson.io. What I want to do is, after I finish drawing a feature I want it to be saved in the data.json file. I am trying to do this by getting the newly created feature by leaflet draw and pushing it to the feature collection object in the json file. I am not quite understanding how I am supposed to do that. create a new object, how do I get the coordinates and the feature type inte the javascript object.

This is the code I use to get the features from the JSON file.

$.getJSON("http://192.168.15.79:8080/PathTO/data.json", function(data){
  L.geoJson(data,{
    style: featureStyles
  }).addTo(map);
});

And this is what I have tried so far

  var drawControl = new L.Control.Draw(drawPluginOptions);
  map.addControl(drawControl)
  var editableLayers = new L.FeatureGroup();
  map.addLayer(editableLayers)
  map.on('draw:created', function(e) {
    var type = e.layerType;
    var layer = e.layer;
    var newFeature = { 
      "type": "features", 
      "geometry": { 
        "type": layer.type,
        "coordinates": layer.getLatLngs()
      //to be continued...
      } 
    };   
    editableLayers.addLayer(e.layer);



   //editableLayers.push(layer);
   // editableLayers.layers.push(serverResponse.featuresOnWs);
  });
  • Almost duplicate of this.gis.stackexchange.com/questions/316407/… However your looking at creating a array of current features, push the new ones, and over write the existing file, which is only on your PC. You might want to research this further and look at a database. – Bill Chappell Apr 3 at 19:23
  • Just edited hope I have been more clear. – silvio progonati Apr 9 at 9:17
  • I think there is function e.layer.toGeoJson() from that you can get the geojson and put that in an array – user4906240 Apr 9 at 9:32
  • What about solutions in this QA: gis.stackexchange.com/questions/237171/… – TomazicM Apr 9 at 11:16
  • Yet i cant quite understand what I am supposed to do here. Should i create an empty array of the object format to be pushed in the geoJSON file, something like: – silvio progonati Apr 9 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.