Search code examples
javascriptjsonajaxgeojsonstringify

Cannot "stringify" geoJSON data


Using Leaflet JavaScript I am trying to pull data directly from GeoServer using an Ajax link. In order to put it nicely in a DataTables table, I need to JSON.stringify it per DataTables instructions. I get a "Circular structure". Is there any other way to do this?

Here is my code:

Get Selected Features from GeoServer via Ajax

function handleJson(data) {

  selectedFeature = L.geoJson(data, {

    onEachFeature: function (feature, layer) {

    },
    pointToLayer: function (feature, latlng) {

        return L.circleMarker(latlng, {
            radius: 5,
            color: '#3006e8',
            weight: 5,
            opacity: 100,
            fillOpacity: 100
        });
    }
  });
  selectedFeature.addTo(drawnItems);

Now here is where I would idealy use JSON.stringify to achieve these results provide by a very helpful person over at datatables.net....

http://live.datatables.net/sokitihe/3/edit

I think var selectedFeature would be the data to JSON.stringify correct? I have tried that and it didn't work.


Solution

  • You can export the GeoJSON LayerGroup directly into a FeatureCollection using:

    selectedGeoJSON = selectedFeature.toGeoJSON();
    

    Then you can do what you want with it, stringify it if you wish.

    JSON.stringify(selectedGeoJSON);