对geobeansEvent事件回调函数的改进
demo中提供的geobeansEvent事件回调函数如下:
function geobeansEvent(id,v1,v2,v3,v4,v5,v6,v7,v8,v9){
var pMap = document.GeoBeansMap;
switch(id){
case 0:
alert("coError:"+id+"\n"+"v1:"+v1+"\n"+"v2:"+v2+"\n"+"v3:"+v3+"\n"+"v4:"+v4+"\n"+"v5:"+v5);
break;
case 7:
//alert("coViewMap:"+id+"\n"+"显示比例:"+v1+"\n"+"XMIN:"+v2+"\n"+"YMIN:"+v3+"\n"+"XMAX:"+v4+"\n"+"YMAX:"+v5);
break;
case 17://完成地图绘制
//alert("coPainted:"+id+"\n"+"v1:"+v1+"\n"+"v2:"+v2+"\n"+"v3:"+v3+"\n"+"v4:"+v4+"\n"+"v5:"+v5);
cc();
break;
case 18://自动停止闪烁
//alert("coFlashed:"+id+"\n"+"v1:"+v1+"\n"+"v2:"+v2+"\n"+"v3:"+v3+"\n"+"v4:"+v4+"\n"+"v5:"+v5);
break;
case 8: //鼠标移动时的图形坐标
//alert("coViewPosition:"+id+"\n"+"X:"+v1+"\n"+"Y:"+v2+"\n"+"v3:"+v3+"\n"+"v4:"+v4);
parent.TOCFrame.setShapeXY(v1,v2);
break;
case 36://鼠标单击事件
//alert("coMouseClick:"+id+"\n"+"X:"+v1+"\n"+"Y:"+v2+"\n"+"v3(ClickCount,x,y):"+v3+"\n"+"v4:"+v4);
parent.TOCFrame.setShapeXY(v1,v3);
break;
case 10://成功加载地图工程
var pManager = pMap.getManager();
var pLayers = document.all("MapConfig");
//动态创建图层配置信息
var strBody="<table width=100% border=1 cellpadding=0 cellspacing=0>";
strBody +="<tr><td><font face=黑体 size=2><strong>地图配置</strong></font></td></tr>";
var nCount=1;
var pNode = pManager.getRootNode();
while(pNode!=null){
strBody +="<tr><td>";
strBody += "<input type=checkbox name=C"+nCount+" value=";
strBody += pNode.getName();
strBody += " onClick=JavaScript:setLayerVisible(this) ";
strBody += (pNode.isVisible()==true)?"checked>":">";
strBody += "<font size=1>";
strBody += pNode.getName();
strBody += "</font>";
strBody += "</td></tr>";
pNode = pNode.getSibling();nCount++;
}
strBody += "</table>";
pLayers.innerHTML= strBody;
var pOverview = document.VEE;
pMap.setOverview(pOverview.getOverview());
pMap.getOptions().setHighlightColorFromString("#0000FF");
break;
case 16://成功加载了MapExpress
//ContextMenu();
break;
case 12://Loading Layer
//alert("开始装载图层:"+id+"\n"+"图层名:"+v1);
break;
case 11:
//alert("装载图层:"+id+"\n"+"图层名:"+v1+"\n"+"v2:"+v2+"\n"+"v3:"+v3+"\n"+"v4:"+v4);
//if(v1=="chinar"){
// pMap.setLayer("Tiper","CHINAR");
//}
//if(v1=="CHINAP")pMap.setLayer("Tiper","CHINAP");
ok=1;parent.TOCFrame.ok=1;
break;
case 19:
//alert("下载子图:"+id+"\n子图号="+v1);
break;
case 21://Overview
alert("coOverview:"+id+"\nVisible="+v1);
break;
case 101:case 102:case 103:case 105:case 106:
//alert("属性查询:"+id+"\n"+"图层名:"+v1+"\n"+"图元个数:"+v2+"\n"+"图元编码:"+v3+"\n"+"v4:"+v4);
if(v2=="-1"){
parent.AttrFrame.location=v3;
}else{
parent.TOCFrame.setSelectedItem(v1,v3,v4);
}
//获得被选中的图形
//var pSelector = pMap.getSelector();
//if(pSelector!=null){alert(""+pSelector);
// var pShapes = pSelector.getShapes();
// if(pShapes)alert(""+pShapes);
//}
break;
case 101+50:
//alert("属性查询:"+id+"\n"+"X坐标:"+v1+"\n"+"Y坐标:"+v2+"\n"+"v3:"+v3+"\n"+"v4=:"+v4);
break;
case 102+50:
alert("属性查询:"+id+"\n"+"圆心坐标X:"+v1+"\n"+"圆心坐标Y:"+v2+"\n"+"半径:"+v3+"\n"+"v4=:"+v4+"\n"+"v5=:"+v5);
break;
case 103+50:
//alert("属性查询:"+id+"\n"+"X最小值:"+v1+"\n"+"Y最小值:"+v2+"\n"+"X最大值:"+v3+"\n"+"Y最大值:"+v4);
break;
case 105+50:
var pDrawer = pMap.getDrawer();
var pPoints = pDrawer.getWorldPoints();
var nCount=pPoints.size();
var strValue = ""+nCount+"(";
for(i=0;i<nCount;i++){
var pPoint=pPoints.dotAt(i);
strValue += pPoint.m_x + " " + pPoint.m_y;
if(i<nCount-1)
strValue += ","
}
strValue += ")";
alert("属性查询:"+id+"\nPoint=" + strValue);
break;
case 301+50:
alert("缓冲区查询:"+id+"\n"+"X坐标:"+v1+"\n"+"Y坐标:"+v2+"\n"+"v3:"+v3+"\n"+"v4=:"+v4);
break;
case 302+50:
alert("缓冲区查询:"+id+"\n"+"圆心坐标X:"+v1+"\n"+"圆心坐标Y:"+v2+"\n"+"半径:"+v3+"\n"+"v4=:"+v4);
break;
case 303+50:
alert("缓冲区查询:"+id+"\n"+"X最小值:"+v1+"\n"+"Y最小值:"+v2+"\n"+"X最大值:"+v3+"\n"+"Y最大值:"+v4);
break;
case 2001:
alert("添加标注点:"+id+"\n"+"图层名:"+v1+"\n"+"X坐标:"+v2+"\n"+"Y坐标:"+v3);
parent.TOCFrame.setShapeXY(v2,v3);
break;
case 2003:
alert("删除标注点:"+id+"\n"+"图层名:"+v1+"\n"+"图元编码:"+v2);
break;
case 2004:
alert("修改标注点:"+id+"\n"+"图层名:"+v1+"\n"+"图元编码:"+v2);
break;
case 202:case 203:
//如果图形是经纬度坐标,可以利用以下代码计算地球表面的距离,单位公里
//var pDrawer = pMap.getDrawer();
//var pPoints = pDrawer.getWorldPoints();
//alert("折线长度:"+pPoints.ellipsoidDistance(1)+"公里");
alert("折线\n长度:"+v1);
break;
case 211:
alert("圆形区域\n面积:"+v1 + "\n周长:"+v2);
break;
case 212:
alert("矩形区域\n面积:"+v1 + "\n周长:"+v2);
break;
case 213:
//如果图形是经纬度坐标,可以利用以下代码计算地球表面的面积,单位平方公里
//var pDrawer = pMap.getDrawer();
//var pPoints = pDrawer.getWorldPoints();
//alert("多边形面积:"+pPoints.ellipsoidArea()+"平方公里");
//alert("多边形区域\n面积:"+v1 + "\n周长:"+v2);
break;
case 214:
alert("面积查询:"+id+"\n"+"图层名:"+v1+"\n"+"图元个数:"+v2+"\n"+"图元面积:"+v3+"\n"+"v4:"+v4);
break;
case 201+50:case 202+50:case 203+50:case 211+50:case 212+50:case 213+50:case 214+50:
alert("地图量算"+id);
break;
//快捷菜单
case 65://coContextMenuPre
//pMap.ContextMenu("aaaaaa=8001;bbbbbb=8002",0,0);
//alert("事件: "+id+"\n"+"v1:"+v1+"\n"+"v2:"+v2+"\n"+"v3:"+v3+"\n"+"v4:"+v4+"\n"+"v5:"+v5);
break;
case 107:
case 8001:
case 8002:
case 8003:
case 8004:
alert("自定义事件: "+id+"\n"+"v1:"+v1+"\n"+"v2:"+v2+"\n"+"v3:"+v3+"\n"+"v4:"+v4+"\n"+"v5:"+v5);
break;
case 23://MouseReleased : v1=x,v2=y
//var pLayer;
//pLayer.setAutoLabel(0x2000);
//var pInfo=pLayer.getLabelInfo(); //此处pInfo为GINote对象,可通过该参数修改标注的字体信息
//pInfo
//alert("MouseReleased");
break;
}
}
var pMap = document.GeoBeansMap;
switch(id){
case 0:
alert("coError:"+id+"\n"+"v1:"+v1+"\n"+"v2:"+v2+"\n"+"v3:"+v3+"\n"+"v4:"+v4+"\n"+"v5:"+v5);
break;
case 7:
//alert("coViewMap:"+id+"\n"+"显示比例:"+v1+"\n"+"XMIN:"+v2+"\n"+"YMIN:"+v3+"\n"+"XMAX:"+v4+"\n"+"YMAX:"+v5);
break;
case 17://完成地图绘制
//alert("coPainted:"+id+"\n"+"v1:"+v1+"\n"+"v2:"+v2+"\n"+"v3:"+v3+"\n"+"v4:"+v4+"\n"+"v5:"+v5);
cc();
break;
case 18://自动停止闪烁
//alert("coFlashed:"+id+"\n"+"v1:"+v1+"\n"+"v2:"+v2+"\n"+"v3:"+v3+"\n"+"v4:"+v4+"\n"+"v5:"+v5);
break;
case 8: //鼠标移动时的图形坐标
//alert("coViewPosition:"+id+"\n"+"X:"+v1+"\n"+"Y:"+v2+"\n"+"v3:"+v3+"\n"+"v4:"+v4);
parent.TOCFrame.setShapeXY(v1,v2);
break;
case 36://鼠标单击事件
//alert("coMouseClick:"+id+"\n"+"X:"+v1+"\n"+"Y:"+v2+"\n"+"v3(ClickCount,x,y):"+v3+"\n"+"v4:"+v4);
parent.TOCFrame.setShapeXY(v1,v3);
break;
case 10://成功加载地图工程
var pManager = pMap.getManager();
var pLayers = document.all("MapConfig");
//动态创建图层配置信息
var strBody="<table width=100% border=1 cellpadding=0 cellspacing=0>";
strBody +="<tr><td><font face=黑体 size=2><strong>地图配置</strong></font></td></tr>";
var nCount=1;
var pNode = pManager.getRootNode();
while(pNode!=null){
strBody +="<tr><td>";
strBody += "<input type=checkbox name=C"+nCount+" value=";
strBody += pNode.getName();
strBody += " onClick=JavaScript:setLayerVisible(this) ";
strBody += (pNode.isVisible()==true)?"checked>":">";
strBody += "<font size=1>";
strBody += pNode.getName();
strBody += "</font>";
strBody += "</td></tr>";
pNode = pNode.getSibling();nCount++;
}
strBody += "</table>";
pLayers.innerHTML= strBody;
var pOverview = document.VEE;
pMap.setOverview(pOverview.getOverview());
pMap.getOptions().setHighlightColorFromString("#0000FF");
break;
case 16://成功加载了MapExpress
//ContextMenu();
break;
case 12://Loading Layer
//alert("开始装载图层:"+id+"\n"+"图层名:"+v1);
break;
case 11:
//alert("装载图层:"+id+"\n"+"图层名:"+v1+"\n"+"v2:"+v2+"\n"+"v3:"+v3+"\n"+"v4:"+v4);
//if(v1=="chinar"){
// pMap.setLayer("Tiper","CHINAR");
//}
//if(v1=="CHINAP")pMap.setLayer("Tiper","CHINAP");
ok=1;parent.TOCFrame.ok=1;
break;
case 19:
//alert("下载子图:"+id+"\n子图号="+v1);
break;
case 21://Overview
alert("coOverview:"+id+"\nVisible="+v1);
break;
case 101:case 102:case 103:case 105:case 106:
//alert("属性查询:"+id+"\n"+"图层名:"+v1+"\n"+"图元个数:"+v2+"\n"+"图元编码:"+v3+"\n"+"v4:"+v4);
if(v2=="-1"){
parent.AttrFrame.location=v3;
}else{
parent.TOCFrame.setSelectedItem(v1,v3,v4);
}
//获得被选中的图形
//var pSelector = pMap.getSelector();
//if(pSelector!=null){alert(""+pSelector);
// var pShapes = pSelector.getShapes();
// if(pShapes)alert(""+pShapes);
//}
break;
case 101+50:
//alert("属性查询:"+id+"\n"+"X坐标:"+v1+"\n"+"Y坐标:"+v2+"\n"+"v3:"+v3+"\n"+"v4=:"+v4);
break;
case 102+50:
alert("属性查询:"+id+"\n"+"圆心坐标X:"+v1+"\n"+"圆心坐标Y:"+v2+"\n"+"半径:"+v3+"\n"+"v4=:"+v4+"\n"+"v5=:"+v5);
break;
case 103+50:
//alert("属性查询:"+id+"\n"+"X最小值:"+v1+"\n"+"Y最小值:"+v2+"\n"+"X最大值:"+v3+"\n"+"Y最大值:"+v4);
break;
case 105+50:
var pDrawer = pMap.getDrawer();
var pPoints = pDrawer.getWorldPoints();
var nCount=pPoints.size();
var strValue = ""+nCount+"(";
for(i=0;i<nCount;i++){
var pPoint=pPoints.dotAt(i);
strValue += pPoint.m_x + " " + pPoint.m_y;
if(i<nCount-1)
strValue += ","
}
strValue += ")";
alert("属性查询:"+id+"\nPoint=" + strValue);
break;
case 301+50:
alert("缓冲区查询:"+id+"\n"+"X坐标:"+v1+"\n"+"Y坐标:"+v2+"\n"+"v3:"+v3+"\n"+"v4=:"+v4);
break;
case 302+50:
alert("缓冲区查询:"+id+"\n"+"圆心坐标X:"+v1+"\n"+"圆心坐标Y:"+v2+"\n"+"半径:"+v3+"\n"+"v4=:"+v4);
break;
case 303+50:
alert("缓冲区查询:"+id+"\n"+"X最小值:"+v1+"\n"+"Y最小值:"+v2+"\n"+"X最大值:"+v3+"\n"+"Y最大值:"+v4);
break;
case 2001:
alert("添加标注点:"+id+"\n"+"图层名:"+v1+"\n"+"X坐标:"+v2+"\n"+"Y坐标:"+v3);
parent.TOCFrame.setShapeXY(v2,v3);
break;
case 2003:
alert("删除标注点:"+id+"\n"+"图层名:"+v1+"\n"+"图元编码:"+v2);
break;
case 2004:
alert("修改标注点:"+id+"\n"+"图层名:"+v1+"\n"+"图元编码:"+v2);
break;
case 202:case 203:
//如果图形是经纬度坐标,可以利用以下代码计算地球表面的距离,单位公里
//var pDrawer = pMap.getDrawer();
//var pPoints = pDrawer.getWorldPoints();
//alert("折线长度:"+pPoints.ellipsoidDistance(1)+"公里");
alert("折线\n长度:"+v1);
break;
case 211:
alert("圆形区域\n面积:"+v1 + "\n周长:"+v2);
break;
case 212:
alert("矩形区域\n面积:"+v1 + "\n周长:"+v2);
break;
case 213:
//如果图形是经纬度坐标,可以利用以下代码计算地球表面的面积,单位平方公里
//var pDrawer = pMap.getDrawer();
//var pPoints = pDrawer.getWorldPoints();
//alert("多边形面积:"+pPoints.ellipsoidArea()+"平方公里");
//alert("多边形区域\n面积:"+v1 + "\n周长:"+v2);
break;
case 214:
alert("面积查询:"+id+"\n"+"图层名:"+v1+"\n"+"图元个数:"+v2+"\n"+"图元面积:"+v3+"\n"+"v4:"+v4);
break;
case 201+50:case 202+50:case 203+50:case 211+50:case 212+50:case 213+50:case 214+50:
alert("地图量算"+id);
break;
//快捷菜单
case 65://coContextMenuPre
//pMap.ContextMenu("aaaaaa=8001;bbbbbb=8002",0,0);
//alert("事件: "+id+"\n"+"v1:"+v1+"\n"+"v2:"+v2+"\n"+"v3:"+v3+"\n"+"v4:"+v4+"\n"+"v5:"+v5);
break;
case 107:
case 8001:
case 8002:
case 8003:
case 8004:
alert("自定义事件: "+id+"\n"+"v1:"+v1+"\n"+"v2:"+v2+"\n"+"v3:"+v3+"\n"+"v4:"+v4+"\n"+"v5:"+v5);
break;
case 23://MouseReleased : v1=x,v2=y
//var pLayer;
//pLayer.setAutoLabel(0x2000);
//var pInfo=pLayer.getLabelInfo(); //此处pInfo为GINote对象,可通过该参数修改标注的字体信息
//pInfo
//alert("MouseReleased");
break;
}
}
使用switch语句对事件id进行分支判断。那么,能否对其进行改造呢,肯定是可以的。希望能去掉这个switch语句,具体实现过程如下:
1、实现一个方法,可以判断某个函数是否存在(相当于反射,根据对象名创建对象的实例,这里采用js实现执行给定名称的函数,当然要利用eval这个有用的方法了),如果存在则执行这个方法。
/**
* 判断名称为给定字符串的一个或多个函数是否存在,执行第一个存在的函数并返回值。
* @param {Array} funcArguments 函数参数
* @arguments {string} 一个或多个函数名称
* @return 函数执行后的返回值
*/
var applyEval = function(funcArguments)
{
for(var i = 1; i < arguments.length; i++)
{
var fName = arguments[i];
if(eval('typeof ' + fName) == "function")
{
return eval(fName + '.apply(this, funcArguments)');
}
}
}
* 判断名称为给定字符串的一个或多个函数是否存在,执行第一个存在的函数并返回值。
* @param {Array} funcArguments 函数参数
* @arguments {string} 一个或多个函数名称
* @return 函数执行后的返回值
*/
var applyEval = function(funcArguments)
{
for(var i = 1; i < arguments.length; i++)
{
var fName = arguments[i];
if(eval('typeof ' + fName) == "function")
{
return eval(fName + '.apply(this, funcArguments)');
}
}
}
2、定义一个geobeans事件哈希表。
var userEventKey = {
Key2: 'OnZoomIn',
Key7: 'OnZoom',
Key10: 'Onloaded',
Key11: 'OnLoadLayer',
Key21: 'OverView',
Key36: 'OnClick',
Key2001:'OnDbClick'
};
Key2: 'OnZoomIn',
Key7: 'OnZoom',
Key10: 'Onloaded',
Key11: 'OnLoadLayer',
Key21: 'OverView',
Key36: 'OnClick',
Key2001:'OnDbClick'
};
示例暂时先只定义这几个事件,可以自己扩展。
3、改造geobeansEvent事件回调函数,使其更利于用户扩展。
var geobeansEvent = function()
{
var keyName = 'Key' + arguments[0].toString();
if(typeof userEventKey != 'undefined' && userEventKey != null)
{
var f1Name = userEventKey[keyName];
applyEval(arguments, f1Name);
}
}
{
var keyName = 'Key' + arguments[0].toString();
if(typeof userEventKey != 'undefined' && userEventKey != null)
{
var f1Name = userEventKey[keyName];
applyEval(arguments, f1Name);
}
}
现在你想写哪个事件的回调函数,只要写userEventKey中对应事件的方法就可以了,比如我想在图层加载事件(事件11,userEventKey中的Key11)回调中进行自定义处理,我只要提供OnLoadLayer方法即可。
以上只是个人的一些改造方法,可能还有其它的好方法,欢迎一起讨论。