您的位置:首页 > 编程语言 > Go语言

Google Maps API 2.0解析(4-公用函数)

2006-05-08 23:39 615 查看
//创建一个以a为NodeName的htmlElement。b,c,d为可选参数,分别为:父对象、位置、大小
function CreateElement(a,b,c,d)
{
var e=GetDocument(b).createElement(a);
if(b)
{
AppendChild(b,e)
}
if(c)SetPosition(e,c);
if(d)SetSize(e,d);
return e
}
//创建文本节点,a试问本内容,b是父节点
function CreateTextNode(a,b)
{
var c=GetDocument(b).createTextNode(a);
if(b)
{
AppendChild(b,c)
}
return c
}
//返回文档对象
function GetDocument(a)
{
return(a?a.ownerDocument:null)||document
}
function GetPixelValue(a)
{
return getMathRound(a)+"px"
}
function GetEMValue(a)
{
return a+"em"
}
//设置HTML控件的位置
function SetPosition(a,b)
{
var c=a.style;
c.position="absolute";
c.left=GetPixelValue(b.x);
c.top=GetPixelValue(b.y)
}
function setLeft(a,b)
{
a.style.left=GetPixelValue(b)
}
//设置控件的大小
function SetSize(a,b)
{
var c=a.style;
c.width=GetPixelValue(b.width);
c.height=GetPixelValue(b.height)
}
function setWidth(a,b)
{
a.style.width=GetPixelValue(b)
}
function setHeight(a,b)
{
a.style.height=GetPixelValue(b)
}
function GetElementById(a)
{
return document.getElementById(a)
}
function setDisplayNone(a)
{
a.style.display="none"
}
function setDisplayNormal(a)
{
a.style.display=""
}
function setVisibilityHidden(a)
{
a.style.visibility="hidden"
}
function setVisibilityNormal(a)
{
a.style.visibility=""
}
function setVisibilityVisible(a)
{
a.style.visibility="visible"
}
function setPositionRelative(a)
{
a.style.position="relative"
}
function setPositionAbsolute(a)
{
a.style.position="absolute"
}
function setOverflowHidden(a)
{
a.style.overflow="hidden"
}
//返回最接近a,而且在b,c范围内的值
function GetNumberInRange(a,b,c)
{
if(b!=null)
{
a=GetMathMax(a,b)
}
if(c!=null)
{
a=GetMathMin(a,c)
}
return a
}
//本函数类似于一个取余的过程
function getLoopNumber(a,b,c)
{
while(a>c)
{
a-=c-b
}
while(a<b)
{
a+=c-b
}
return a
}
function getMathRound(a)
{
return Math.round(a)
}
function getMathFloor(a)
{
return Math.floor(a)
}
function ib(a)
{
return Math.ceil(a)
}
//获取最大值
function GetMathMax(a,b)
{
return Math.max(a,b)
}
//获取最小值
function GetMathMin(a,b)
{
return Math.min(a,b)
}
function getMathAbs(a)
{
return Math.abs(a)
}
//设置鼠标样式
function setCursor(a,b)
{
try
{
a.style.cursor=b
}
catch(c)
{
if(b=="pointer")
{
setCursor(a,"hand")
}
}
}
//阻止事件默认行为的执行
function PreventEventDefault(a)
{
if(browser.type==1)
{
window.event.cancelBubble=true;
window.event.returnValue=false
}
else
{
a.preventDefault();
a.stopPropagation()
}
}
//取消事件冒泡
function CancelEventBubble(a)
{
if(browser.type==1)
{
window.event.cancelBubble=true
}
else
{
a.stopPropagation()
}
}
function SetNoPrint(a)
{
a.className="gmnoprint"
}
function setNoScreen(a)
{
a.className="gmnoscreen"
}
function setZIndex(a,b)
{
a.style.zIndex=b
}
//变量是否已经定义
function isDefined(a)
{
return typeof a!="undefined"
}
function isNumber(a)
{
return typeof a=="number"
}
//c毫秒后执行a的b方法
function setWindowTimeOut(a,b,c)
{
return window.setTimeout(function()
{
b.apply(a)
},c)
}

//计算两个HTML控件(b包含a)之间的位置差距
function getOffset(a,b)
{
var c=new GPoint(0,0);
while(a&&a!=b)
{
if(a.nodeName=="BODY")
{
getBodyOffset(c,a)
}
var d=getBorderSize(a);
c.x+=d.width;
c.y+=d.height;
if(a.nodeName!="BODY"||!browser.isFirefox())
{
c.x+=a.offsetLeft;
c.y+=a.offsetTop
}
if(browser.isFirefox()&&browser.revision>=1.8&&a.offsetParent&&a.offsetParent.nodeName!="BODY"&&getStyleValue(a.offsetParent,"overflow")!="visible")
{
var d=getBorderSize(a.offsetParent);
c.x+=d.width;
c.y+=d.height
}
if(a.offsetParent)
{
c.x-=a.offsetParent.scrollLeft;
c.y-=a.offsetParent.scrollTop
}
if(browser.type!=1&&Yd(a))
{
if(browser.isFirefox())
{
c.x-=self.pageXOffset;
c.y-=self.pageYOffset;
var e=getBorderSize(a.offsetParent.parentNode);
c.x+=e.width;
c.y+=e.height
}
break
}
if(browser.type==2&&a.offsetParent)
{
var d=getBorderSize(a.offsetParent);
c.x-=d.width;
c.y-=d.height
}
a=a.offsetParent
}
if(browser.type==1&&!b&&document.documentElement)
{
c.x+=document.documentElement.clientLeft;
c.y+=document.documentElement.clientTop
}
if(b&&a==null)
{
var f=getOffset(b);
return new GPoint(c.x-f.x,c.y-f.y)
}
else
{
return c
}
}
function Yd(a)
{
if(a.offsetParent&&a.offsetParent.nodeName=="BODY"&&getStyleValue(a.offsetParent,"position")=="static")
{
if(browser.type==0&&getStyleValue(a,"position")!="static")
{
return true
}
else if(browser.type!=0&&getStyleValue(a,"position")=="absolute")
{
return true
}
}
return false
}
//获取Body对象的margin和border所使用的像素大小
function getBodyOffset(a,b)
{
var c=false;
if(browser.isFirefox())
{
c=getStyleValue(b,"overflow")!="visible"&&getStyleValue(b.parentNode,"overflow")!="visible";
var d=getStyleValue(b,"position")!="static";
if(d||c)
{
a.x+=getCssDummy(b,"margin-left");
a.y+=getCssDummy(b,"margin-top");
var e=getBorderSize(b.parentNode);
a.x+=e.width;
a.y+=e.height
}
if(d)
{
a.x+=getCssDummy(b,"left");
a.y+=getCssDummy(b,"top")
}
}
if((browser.isFirefox()||browser.type==1)&&document.compatMode!="BackCompat"||c)
{
if(self.pageYOffset)
{
a.x-=self.pageXOffset;
a.y-=self.pageYOffset
}
else
{
a.x-=document.documentElement.scrollLeft;
a.y-=document.documentElement.scrollTop
}
}
}
//获取一个HTML对象的clientX和clientY
function getClientPoint(a)
{
if(browser.type==2)
{
return new GPoint(a.pageX-self.pageXOffset,a.pageY-self.pageYOffset)
}
else
{
return new GPoint(a.clientX,a.clientY)
}
}
//获取事件a发生处相对于b的位置
function getEventOffset(a,b)
{
if(isDefined(a.offsetX))
{
var c=getEventTarget(a);
var d=getOffset(c,b);
var e=new GPoint(a.offsetX,a.offsetY);
if(browser.type==2)
{
var f=getBorderSize(c);
e.x-=f.width;
e.y-=f.height
}
return new GPoint(d.x+e.x,d.y+e.y)
}
else if(isDefined(a.clientX))
{
var g=getClientPoint(a);
var h=getOffset(b);
return new GPoint(g.x-h.x,g.y-h.y)
}
else
{
return GPoint.ORIGIN
}
}
//获取事件的触发源
function getEventTarget(a)
{
var b=a.target||a.srcElement;
if(b.nodeType==3)
{
b=b.parentNode
}
return b
}
//从数据a之中删除b项,返回删除的个数
function RemoveFromArray(a,b,c)
{
var d=0;
for(var e=0;e<a.length;++e)
{
if(a[e]===b||c&&a[e]==b)
{
a.splice(e--,1);
d++
}
}
return d
}
//向数组a添加一个项d,如果存在相同项目,则不添加并返回false
function addToArray(a,b,c)
{
for(var d=0;d<a.length;++d)
{
if(a[d]===b||c&&a[d]==b)
{
return false
}
}
a.push(b);
return true
}
//将b对象设置为a的子项
function AppendChild(a,b)
{
a.appendChild(b)
}
//从文档之中删除a
function deposeNode(a)
{
if(a.parentNode)
{
a.parentNode.removeChild(a);
clearNode(a)
}
}
//清空a的所有子对象
function removeNodeChildren(a)
{
var b;
while(b=a.firstChild)
{
clearNode(b);
a.removeChild(b)
}
}
//设置a的innerHTML
function setInnerHTML(a,b)
{
if(a.innerHTML!=b)
{
removeNodeChildren(a);
a.innerHTML=b
}
}
//设置a不可被用户选中
function SetUnSelectable(a)
{
if(browser.isFirefox())
{
a.style.MozUserSelect="none"
}
else
{
a.unselectable="on";
a.onselectstart=falseFunction
}
}
//以多种参数执行b的c方法,参数列表在a之中
function runMethodByArgumentsArray(a,b,c)
{
for(var d=0;d<a.length;d++)
{
c.call(b,a[d],d)
}
}
//为多个对象(对象数组a)执行b方法,取返回结果的最大值或最小值
//如果c为Math.max,则为最大值
//如果c为Math.min,则为最小值
function getComparedMethodResult(a,b,c)
{
var d;
for(var e=0;e<a.length;++e)
{
var f=b.apply(a[e]);
if(e==0)
{
d=f
}
else
{
d=c(d,f)
}
}
return d
}
//为数组a之中的每一个项目运行b方法
function RunMethodForAll(a,b)
{
for(var c=0;c<a.length;++c)
{
b.call(a[c])
}
}
//为数组a之中的每一个项目运行b方法,并将运行结果以数组的方式返回
function getMethodResultArray(a,b)
{
var c=[];
for(var d=0;d<a.length;++d)
{
c.push(b.call(a[d]))
}
return c
}
//将数组b的一段内容复制到数组a之中
function CopyArray(a,b,c,d)
{
var e=c||0;
var f=d||b.length;
for(var g=e;g<f;++g)
{
a.push(b[g])
}
}
function falseFunction()
{
return false
}
//格式化经纬度数字
function formatLatLngNumber(a)
{
var b=Math.round(a*1000000)/1000000;
return b.toString()
}
//返回弧度
function getRadianByDegree(a)
{
return a*GMath_PI/180
}
//返回角度
function getDegreeByRadian(a)
{
return a/(GMath_PI/180)
}
//判断两点坐标之间的距离是否小到可以认为是同一个坐标点
function isSameCoordinatePoint(a,b)
{
return getMathAbs(a-b)<=1.0E-9
}

//设置html对象的不透明度
function setObjectOpacity(a,b)
{
if(browser.type==1)
{
a.style.filter="alpha(opacity="+getMathRound(b*100)+")"
}
else
{
a.style.opacity=b
}
}
//创建一个背景为黑色,半透明层,在GLog里面使用
function Md(a,b,c)
{
var d=CreateElement("div",a,b,c);
d.style.backgroundColor="black";
setObjectOpacity(d,0.35);
return d
}
//返回Style属性
function getStyleValue(a,b)
{
var c=GetDocument(a);
if(a.currentStyle)
{
var d=getCssName(b);
return a.currentStyle[d]
}
else if(c.defaultView&&c.defaultView.getComputedStyle)
{
var e=c.defaultView.getComputedStyle(a,"");
return e?e.getPropertyValue(b):""
}
else
{
var d=getCssName(b);
return a.style[d]
}
}
var Bc="__mapsBaseCssDummy__";
//本方法是Google地图制作精细的表现,本方法的目的是获取一个层的像素属性大小,Google为了能在所有浏览器里面都能有最好的效果,特意用本函数来避免浏览器处理不一致的情况
//比如同样是margin,IE和Firefox就会有细微的不同的,这个不同大家制作网页的时候经常忽略,可是Google考虑到了
function getCssDummy(a,b,c)
{
var d;
if(c)
{
d=c
}
else
{
d=getStyleValue(a,b)
}
if(isNumber(d))
{
return d
}
else if(isNaN(parseInt(d)))
{
return d
}
else if(d.length>2&&d.substring(d.length-2)=="px")
{
return parseInt(d)
}
else
{
var e=a.ownerDocument.getElementById(Bc);
if(!e)
{
var e=CreateElement("div",a,new GPoint(0,0),new GSize(0,0));
e.id=Bc;
setVisibilityHidden(e)
}
else
{
a.parentNode.appendChild(e)
}
e.style.width="0px";
e.style.width=d;
return e.offsetWidth
}
}
var Cc="border-left-width";
var Dc="border-top-width";
//返回对象的border占用的像素大小
function getBorderSize(a)
{
var b,c;
var d=getStyleValue(a,Cc);
if(isNaN(parseInt(d)))
{
b=0
}
else
{
b=getCssDummy(a,Cc,d)
}
var e=getStyleValue(a,Dc);
if(isNaN(parseInt(e)))
{
c=0
}
else
{
c=getCssDummy(a,Dc,e)
}
return new GSize(b,c)
}
//这个函数的作用是将一个CSS属性名称变换JavaScript的引用名称,例如将font-size变换为fontSize
//看到这里,我对Google的API开发人员的佩服又进了一层,首先是我从来不知道,replace函数还可以用function做参数的
//其次是这么简单的转化方式我闻所未闻,要是我,肯定要做一个映射表了
function getCssName(a)
{
return a.replace(/-(\w)/g,function(b,c)
{
return(""+c).toUpperCase()
}
)
}
//取消a包涵的所有输入框的输入焦点
function blurChildrenInput(a)
{
RunFunction(a,function(b)
{
if(b.nodeName=="INPUT"&&b.getAttribute("type").toLowerCase()=="text"||b.nodeName=="TEXTAREA")
{
try
{
b.blur()
}
catch(c)
{
}
}
}
)
}
//将b的原型赋给a
function InheritClass(a,b)
{
var c=function()
{
};
c.prototype=b.prototype;
a.prototype=new c()
};

本节比较长,都是一些公用的函数,这些函数并没有对外开放,不过却是非常重要的,有些函数的存在只是为了代码混淆,而有些则是为了代码的清晰和可维护性而存在的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: