0

I'm trying to save an array consisting of multiple features via localStorage.

var featureArray=[];
map.on('click', function(evt) {
  if (feature) {
    featureArray.push(feature.values_);
  }
});

function saveFeatures() {
  localStorage.setItem('features', featureArray);
}
saveFeatures();

Via console console.log(featureArray); looks fine, but when I try to load them via localStorage.getItem('features') the output (after two clicks) is something like:

[object Object],[object Object]

But I actually want the values behind this structure to be saved:

enter image description here

I tried JSON.stringify(featureArray) but that throws the error

TypeError: Converting circular structure to JSON

What am I doing wrong?

UPDATE: https://jsfiddle.net/ytc26fju/3/ (hover over the dot, then save and then try loading with the buttons

1

Got it. Don't fully understand why, but a feature Object had a circular structure. I had to do:

for (i=0;i<featureArray.length;i++) {
  delete featureArray[i].geometry;
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.