//滑动地图操作的键码值
var moveKeyCodes=[37,38,39,40];
//每个码值对应的方向
var moveKeyDirection=
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
38:[0,1],40:[0,-1],37:[1,0],39:[-1,0]
};
//支持键盘操作的类,b参数是要将事件绑定到的层,默认是document
function GKeyboardHandler(map,b)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
this.map=map;
bindDom(window,"blur",this,this.onBlur);
this.addKeyboardEvent(b)
}
GKeyboardHandler.prototype.addKeyboardEvent=function(a)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
var b=a||document;
if(browser.isFirefox()&&browser.os==1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bindDom(b,"keydown",this,this.onKeyPress);
bindDom(b,"keypress",this,this.onKeyDown)
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bindDom(b,"keydown",this,this.onKeyDown);
bindDom(b,"keypress",this,this.onKeyPress)
}
bindDom(b,"keyup",this,this.onKeyUp);
this.moveDirection=
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
};
GKeyboardHandler.prototype.onKeyDown=function(a)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if(this.isMapEvent(a))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true
}
var b=this.map;
switch(a.keyCode)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//方向键,记录下移动方向,然后开始移动
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键的支持
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if(this.isMapEvent(a))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true
}
switch(a.keyCode)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case 61:case 43:case 45:case 95:PreventEventDefault(a);
return false
}
return true
};
//键盘放开的时候停止移动
GKeyboardHandler.prototype.onKeyUp=function(a)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
switch(a.keyCode)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case 38:case 40:case 37:case 39:this.moveDirection[a.keyCode]=null;
return false
}
return true
};
//判断改键盘事件是否应该由地图处理
GKeyboardHandler.prototype.isMapEvent=function(a)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if(a.ctrlKey||a.altKey||a.metaKey)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true
}
var b=GetEventElement(a);
if(b&&(b.nodeName=="INPUT"&&b.getAttribute("type").toLowerCase()=="text"||b.nodeName=="TEXTAREA"))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true
}
return false
};
//开始滑动地图
GKeyboardHandler.prototype.startMove=function()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
var a=this.map;
if(!a.isLoaded())return;
a.endMove();//如果地图已经正在滑动,则先停止
trigger(a,GEvent_movestart);
if(!this.zc)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.ticks=new GTicks(100);
this.moveMap()
}
};
//不停的调用该方法会滑动地图
GKeyboardHandler.prototype.moveMap=function()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
var a=this.moveDirection;
var b=0;
var c=0;
var d=false;
//根据当前已经按下的键确定华东方向
for(var e=0;e<moveKeyCodes.length;e++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(a[moveKeyCodes[e]])
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var f=moveKeyDirection[moveKeyCodes[e]];
b+=f[0];
c+=f[1];
d=true
}
}
var g=this.map;
//向在指定方向滑动
if(d)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var h=1;
var i=browser.type!=0||browser.os!=1;
if(i&&this.ticks.more())
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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//如果当前没有任何键被按下,则完成滑动
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.zc=null;
trigger(g,GEvent_moveend)
}
};
//用户窗口失去焦点的时候停止滑动
GKeyboardHandler.prototype.onBlur=function(a)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
this.moveDirection=
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
};