简体   繁体   中英

How can I turn the Google Maps API Map object into a JSON string for storage in local storage?

I am working with the google maps API and I am trying to convert the map object into a JSON string to store it in local storage. For some reason when I run this block of code the second console.log never runs and when I look in local storage it still says [object Object] meaning the JSON.strigify() never worked.

map = new window.google.maps.Map(document.getElementById('map'), {
    center: this.state.center,
    zoom: 8.5
});
console.log(map);
let jsonMap = JSON.stringify(map);
console.log(jsonMap);

I could be missing something very obvious but I just can't see it.

Cheers,

You will get a type error trying to stringify the map. It is pretty complex in structure and content.

Why are you trying to save it to a local storage anyway? You would be better saving the options that build the map instead of the map object itself ie latitude, longitude, zoom etc.

If you really need to go down the stringify route check this post out:

JSON.stringify, avoid TypeError: Converting circular structure to JSON

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM