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.