简体   繁体   English

如何将Google Maps API Map对象转换为JSON字符串以存储在本地存储中?

[英]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. 我正在使用Google Maps API,并且正在尝试将地图对象转换为JSON字符串以将其存储在本地存储中。 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. 出于某种原因,当我运行此代码块时,第二个console.log永远不会运行,而当我查看本地存储时,它仍然显示[object Object],这意味着JSON.strigify()永远无法工作。

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 JSON.stringify,避免TypeError:将圆形结构转换为JSON

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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