0

I'm attempting to store a Google Map's center point to a cookie and restore the map to the last position the next time the page is loaded.

Sure I can save the latitude and longitude into the cookie as a string or a serialized array or something but I'm curious if I can just JSON serialize the Google Maps LatLng object to the cookie and then just restore it at a later time.

I'm using JQuery and this library: http://code.google.com/p/cookies/

I set the cookie with a LatLng from the map:

$.cookies.set( 'map_center', map.getCenter());

And Then I try to read it out expecting the serialization/deserialization will restore my object...

var centerpointFromCookie = $.cookies.get('map_center);
map.setCenter(centerpointFromCookie);

But it appears the object type is not preserved. In the script console this is what the LatLng Object looks like before it is serialized and stored in the cookie:

map.getCenter()
O
$a: 151.21950000000004
Za: -33.8688
__proto__: O
constructor: function O(a, b, c) {a-=0;b-=0;c||(a=Bd(a,-90,90),b=Cd(b,-180,180));this.Za=a;this.$a=b;}
equals: function (a) {return!a?k:Dd(this.lat(),a.lat())&&Dd(this.lng(),a.lng());}
lat: function () {return this[a];}
lng: function () {return this[a];}
toString: function () {return"("+this.lat()+", "+this.lng()+")";}
toUrlValue: function (a) {a=Hd(a)?a:6;return ae(this.lat(),a)+","+ae(this.lng(),a);}
__proto__: Object

And this is what it looks like afterwards:

centerpointFromCookie
Object
$a: 151.21950000000004
Za: -33.8688
__proto__: Object
__defineGetter__: function __defineGetter__() {
__defineSetter__: function __defineSetter__() {
__lookupGetter__: function __lookupGetter__() {
__lookupSetter__: function __lookupSetter__() {
constructor: function Object() {
hasOwnProperty: function hasOwnProperty() {
isPrototypeOf: function isPrototypeOf() {
propertyIsEnumerable: function propertyIsEnumerable() {
toLocaleString: function toLocaleString() {
toString: function toString() {
valueOf: function valueOf() {

Do I need to cast the object to the GoogleMapsLatLng type when deserializing? I know I could just store the values but I'd like to understand how to properly serialize a small object like this out to a cookie.

4

2 に答える 2

2

Just serialize the lat/lng.

Serializing the object's prototype or function properties doesn't work well because they have to be stringified for that, and when you try to turn the string back into a function, you will lose all references to variables that were visible in the original scope but are not visible now. And you can't easily guess how to manually restore the right prototype later, because prototypes can change at any time. And you'll be tying your object to that specific version of the LatLng prototype which will not be fun when the API gets updated.

Not to mention serializing that much data will take up a lot more space in the cookie than plain lat/lng.

于 2012-04-20T20:22:11.073 に答える
1

getCenter()すでにあなたにただの価値を与えています。したがって、それをJSON文字列に変換し、再度使用するときに解析することができます。

var center = JSON.stringify(map.getCenter());
$.cookies.set( 'map_center', center);

そして、あなたがクッキーから読みたいとき;

var centerpointFromCookie = $.cookies.get('map_center);
map.setCenter(JSON.parse(centerpointFromCookie));
于 2012-04-20T20:24:46.403 に答える