开发者

equivalent to GMap2.savePosition in v3 Maps API?

I'm in the process of converting some google maps code (that i didn't write) that use开发者_如何学Cs GMap2.savePosition(). is there an equivelent method, or better preferred way to do this in the v3 api?


Googled around and couldn't find a replacement in the v3 spec, but it's not hard to do a replacement yourself, either in page or with a cookie.

1) Page code

var myPos, myZoom;
function savePos() {
    myPos = map.getCenter();
    myZoom = map.getZoom();
}

function restorePos() {
    map.setCenter(myPos);
    map.setZoom(myZoom);
}

2) Using cookies

Taken from this example

function Save() { 
    var mapzoom = map.getZoom(); 
    var mapcenter = map.getCenter(); 
    var maplat = mapcenter.lat(); 
    var maplng = mapcenter.lng(); 
    var cookiestring = maplat + "_" + maplng + "_" + mapzoom; 
    var exp = new Date(); 
    //set new date object 
    exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 30)); 
    //set it 30 days ahead 
    setCookie("DaftLogicGMRLL",cookiestring, exp); 
} 

function Load() { 
    var loadedstring=getCookie("DaftLogicGMRLL"); 
    var splitstr = loadedstring.split("_"); 
    map.setCenter(new google.maps.LatLng(parseFloat(splitstr[0]), parseFloat(splitstr[1])));
    map.setZoom(parseFloat(splitstr[2]));
}

function setCookie(name, value, expires) {
    document.cookie = name + "=" + escape(value) + "; \
        path=/" + ((expires == null) ? "" : "; \
        expires=" + expires.toGMTString()); 
} 

function getCookie(c_name) { 
    if (document.cookie.length>0) { 
        c_start=document.cookie.indexOf(c_name + "="); 
        if (c_start!=-1) { 
            c_start=c_start + c_name.length+1; 
            c_end=document.cookie.indexOf(";",c_start); 
            if (c_end==-1) c_end=document.cookie.length; 
            return unescape(document.cookie.substring(c_start,c_end)); 
        } 
    } 
return ""; 
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜