K_Reverter的网页开发记录

要么不做,要么就当作艺术品来做!

导航

Google Maps API 2.0解析(11-GKeyboardHandler支持键盘操作的类)

        //滑动地图操作的键码值
        var moveKeyCodes=[37,38,39,40];
        
//每个码值对应的方向
        var moveKeyDirection=
        
{
            
38:[0,1],40:[0,-1],37:[1,0],39:[-1,0]
        }
;
        
//支持键盘操作的类,b参数是要将事件绑定到的层,默认是document
        function GKeyboardHandler(map,b)
        
{
            
this.map=map;
            bindDom(window,
"blur",this,this.onBlur);
            
this.addKeyboardEvent(b)
        }

        GKeyboardHandler.prototype.addKeyboardEvent
=function(a)
        
{
            
var b=a||document;
            
if(browser.isFirefox()&&browser.os==1)
            
{
                bindDom(b,
"keydown",this,this.onKeyPress);
                bindDom(b,
"keypress",this,this.onKeyDown)
            }

            
else
            
{
                bindDom(b,
"keydown",this,this.onKeyDown);
                bindDom(b,
"keypress",this,this.onKeyPress)
            }

            bindDom(b,
"keyup",this,this.onKeyUp);
            
this.moveDirection=
            
{
            }

        }
;
        GKeyboardHandler.prototype.onKeyDown
=function(a)
        
{
            
if(this.isMapEvent(a))
            
{
                
return true
            }

            
var b=this.map;
            
switch(a.keyCode)
            
{
                
//方向键,记录下移动方向,然后开始移动
                case 38:case 40:case 37:case 39:this.moveDirection[a.keyCode]=1;
                
this.startMove();
                PreventEventDefault(a);
                
return false;
                
//pageUp,pageDown,home,end四个键,直接移动地图
                case 34:b.panBy(new GSize(0,-getMathRound(b.getSize().height*0.75)));
                PreventEventDefault(a);
                
return false;
                
case 33:b.panBy(new GSize(0,getMathRound(b.getSize().height*0.75)));
                PreventEventDefault(a);
                
return false;
                
case 36:b.panBy(new GSize(getMathRound(b.getSize().width*0.75),0));
                PreventEventDefault(a);
                
return false;
                
case 35:b.panBy(new GSize(-getMathRound(b.getSize().width*0.75),0));
                PreventEventDefault(a);
                
return false;
                
//大小键盘上的+-
                case 187:case 107:b.zoomIn();
                PreventEventDefault(a);
                
return false;
                
case 189:case 109:b.zoomOut();
                PreventEventDefault(a);
                
return false
            }

            
switch(a.which)//fireFox键的支持
            {
                
case 61:case 43:b.zoomIn();
                PreventEventDefault(a);
                
return false;
                
case 45:case 95:b.zoomOut();
                PreventEventDefault(a);
                
return false
            }

            
return true
        }
;
        GKeyboardHandler.prototype.onKeyPress
=function(a)
        
{
            
if(this.isMapEvent(a))
            
{
                
return true
            }

            
switch(a.keyCode)
            
{
                
case 38:case 40:case 37:case 39:case 34:case 33:case 36:case 35:case 187:case 107:case 189:case 109:PreventEventDefault(a);
                
return false
            }

            
switch(a.which)
            
{
                
case 61:case 43:case 45:case 95:PreventEventDefault(a);
                
return false
            }

            
return true
        }
;
        
//键盘放开的时候停止移动
        GKeyboardHandler.prototype.onKeyUp=function(a)
        
{
            
switch(a.keyCode)
            
{
                
case 38:case 40:case 37:case 39:this.moveDirection[a.keyCode]=null;
                
return false
            }

            
return true
        }
;
        
//判断改键盘事件是否应该由地图处理
        GKeyboardHandler.prototype.isMapEvent=function(a)
        
{
            
if(a.ctrlKey||a.altKey||a.metaKey)
            
{
                
return true
            }

            
var b=GetEventElement(a);
            
if(b&&(b.nodeName=="INPUT"&&b.getAttribute("type").toLowerCase()=="text"||b.nodeName=="TEXTAREA"))
            
{
                
return true
            }

            
return false
        }
;
        
//开始滑动地图
        GKeyboardHandler.prototype.startMove=function()
        
{
            
var a=this.map;
            
if(!a.isLoaded())return;
            a.endMove();
//如果地图已经正在滑动,则先停止
            trigger(a,GEvent_movestart);
            
if(!this.zc)
            
{
                
this.ticks=new GTicks(100);
                
this.moveMap()
            }

        }
;
        
//不停的调用该方法会滑动地图
        GKeyboardHandler.prototype.moveMap=function()
        
{
            
var a=this.moveDirection;
            
var b=0;
            
var c=0;
            
var d=false;
            
//根据当前已经按下的键确定华东方向
            for(var e=0;e<moveKeyCodes.length;e++)
            
{
                
if(a[moveKeyCodes[e]])
                
{
                    
var f=moveKeyDirection[moveKeyCodes[e]];
                    b
+=f[0];
                    c
+=f[1];
                    d
=true
                }

            }

            
var g=this.map;
            
//向在指定方向滑动
            if(d)
            
{
                
var h=1;
                
var i=browser.type!=0||browser.os!=1;
                
if(i&&this.ticks.more())
                
{
                    h
=this.ticks.next()
                }

                
var l=getMathRound(7*h*5*b);
                
var n=getMathRound(7*h*5*c);
                
var p=g.getDragObject();
                p.moveTo(p.left
+l,p.top+n);
                
//0.01秒之后再次运行移动过程
                this.zc=setWindowTimeOut(this,this.moveMap,10)
            }

            
else//如果当前没有任何键被按下,则完成滑动
            {
                
this.zc=null;
                trigger(g,GEvent_moveend)
            }

        }
;
        
//用户窗口失去焦点的时候停止滑动
        GKeyboardHandler.prototype.onBlur=function(a)
        
{
            
this.moveDirection=
            
{
            }

        }
;

posted on 2006-08-18 17:13  K_Reverter  阅读(1968)  评论(2编辑  收藏  举报