Google Maps API function map.getCenter()

家住魔仙堡 提交于 2019-12-22 03:48:47

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/9287083/google-maps-api-function-map-getcenter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!