Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps API function map.getCenter()

I'm saving the Zoom and Location of the Google Map API setting in cookies as the user adjusts his map. When they come back the map is at the same place. The function works most of the time:

   var h = JSON.stringify(map.getCenter(), null, 2);
   jQuery.cookies.set("YD44635center",h,cookieOptions);

On the decode side using:

    locationVar = jQuery.cookies.get("YD44635center");
    var temp = "";
    // for testing:
    for(var x in locationVar){
        temp += x + "\n";
    }
    alert(temp);

To see what I'm getting, most of the time, is:

   Qa;
   Pa;

So I set my code to load the map with those variables and everything is fine. Then one day the page stops working and the parameters returned do not have a "Q" anymore like in Qa but an "O" like in Oa. So I changed the code and it worked for a day and then what Google was sending changed back to the Qa. I changed it back.

Time goes by. Now today the code start working intermittently and I put that debug thing back in and now instead of "Pa" on the second variable I'm getting "Ra". Not continuously but mostly. What's up. It's happening on two different browsers the same way.

like image 904
Bob Brunius Avatar asked Feb 15 '12 02:02

Bob Brunius


People also ask

How do you find the center of a map?

Google maps has a simple way to do this: center = map. getCenter.

How do you get bounds on Google Maps?

getBounds() in Google Maps API v3 But in API v3 you will get “bounds is undefined” error. So to get our latitude and longitude we need to move getBounds(), to some event listener. Description of bounds_changed in documentation is: “This event is fired when the viewport bounds have changed.”

What does fitBounds do in Google Maps?

Sets the viewport to contain the given bounds. Note: When the map is set to display: none , the fitBounds function reads the map's size as 0x0, and therefore does not do anything. To change the viewport while the map is hidden, set the map to visibility: hidden , thereby ensuring the map div has an actual size.


1 Answers

Use API functions and save the required data, not the structure

var c = map.getCenter();
jQuery.cookies.set("YD44635center", c.lat() + ',' 
                                  + c.lng() + ',' + map.getZoom(), 
                                                     cookieOptions);

and read it as

var temp = jQuery.cookies.get("YD44635center").split(',');

Google is changing the names of the internal variables from time to time Error on Latitude and Longitude - Google Maps API JS 3.0

like image 110
Cheery Avatar answered Sep 21 '22 04:09

Cheery