相当于v3 Maps API中的GMap2.savePosition? [英] equivalent to GMap2.savePosition in v3 Maps API?

查看:71
本文介绍了相当于v3 Maps API中的GMap2.savePosition?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在转换一些使用GMap2.savePosition()的谷歌地图代码(我没写)。是否有一个等价的方法,或者更好的方法来做到这一点在v3 api?

I'm in the process of converting some google maps code (that i didn't write) that uses GMap2.savePosition(). is there an equivelent method, or better preferred way to do this in the v3 api?

推荐答案

搜索并找不到这是v3规范中的一个替代品,但是你可以自己做一个替换,不管是在页面还是用cookie。

1)页面代码



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.

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

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



2)使用cookies



摘自这个例子

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 ""; 
} 

这篇关于相当于v3 Maps API中的GMap2.savePosition?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆