OpenLayers学习9-元素Element,经纬度LngLat,尺寸Size Posted on 2015-02-05 | In OpenLayers学习 | Element不是一个类(不是从Class扩展出来的), 感觉是对Dom操作的包装对象.看了一下类图, Element仅仅作为SVG和VML的父类使用, 这样组织的好处还没体会到 一:Element123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111//说明依赖于哪些类/** * @requires OpenLayers/Util.js * @requires OpenLayers/BaseTypes.js */ //仅仅是个Namespace/** * Namespace: OpenLayers.Element */OpenLayers.Element = { //查询元素的style.display是否为'none' visible: function(element) { return OpenLayers.Util.getElement(element).style.display != 'none'; }, //切换传入domID的显示隐藏 toggle: function() { for (var i=0, len=arguments.length; i<len; i++) { var element = OpenLayers.Util.getElement(arguments[i]); var display = OpenLayers.Element.visible(element) ? 'none' : ''; element.style.display = display;//切换显示状态 } }, //从parentNode里removeChild remove: function(element) { element = OpenLayers.Util.getElement(element); element.parentNode.removeChild(element); }, //获得目标dom的offsetHeight(height + padding值 + border值) getHeight: function(element) { element = OpenLayers.Util.getElement(element); return element.offsetHeight; }, //测试className中是否包含传入的name hasClass: function(element, name) { var names = element.className; return (!!names && new RegExp("(^|\\s)" + name + "(\\s|$)").test(names)); }, //如果没有此class, 添加 addClass: function(element, name) { if(!OpenLayers.Element.hasClass(element, name)) { element.className += (element.className ? " " : "") + name; } return element; }, //从元素的中, 删掉传入的类名(正则换成空格) removeClass: function(element, name) { var names = element.className; if(names) { element.className = OpenLayers.String.trim( names.replace( new RegExp("(^|\\s+)" + name + "(\\s+|$)"), " " ) ); } return element; }, //切换class, 没有加上, 有就删掉 toggleClass: function(element, name) { if(OpenLayers.Element.hasClass(element, name)) { OpenLayers.Element.removeClass(element, name); } else { OpenLayers.Element.addClass(element, name); } return element; }, // getStyle: function(element, style) { element = OpenLayers.Util.getElement(element); var value = null; if (element && element.style) { //为了兼容性, 驼峰处理传入的style value = element.style[OpenLayers.String.camelize(style)]; if (!value) { if (document.defaultView && document.defaultView.getComputedStyle) { //优选使用document.defaultView.getComputedStyle去取style var css = document.defaultView.getComputedStyle(element, null); value = css ? css.getPropertyValue(style) : null; } else if (element.currentStyle) { //element.currentStyle是低版本IE才有的 value = element.currentStyle[OpenLayers.String.camelize(style)]; } } var positions = ['left', 'top', 'right', 'bottom']; //特殊处理opera, 传入的是positions[]且position是static if (window.opera && (OpenLayers.Util.indexOf(positions,style) != -1) && (OpenLayers.Element.getStyle(element, 'position') == 'static')) { value = 'auto'; } } return value == 'auto' ? null : value; } //这里没有CLASS_NAME //看了一下类图, Element仅仅作为SVG和VML的父类使用}; 二: 经纬度LonLat123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100//经纬度类 OpenLayers.LonLat = OpenLayers.Class({ //经度 横向, 公司内部规定缩写成lng的 lon: 0.0, //维度 纵向 lat: 0.0, //构造函数, lon可以是数组[lon,lat] initialize: function(lon, lat) { if (OpenLayers.Util.isArray(lon)) { lat = lon[1]; lon = lon[0]; } this.lon = OpenLayers.Util.toFloat(lon); this.lat = OpenLayers.Util.toFloat(lat); }, //toString toString:function() { return ("lon=" + this.lon + ",lat=" + this.lat); }, //化简掉toString里的描述,只留数字 toShortString:function() { return (this.lon + ", " + this.lat); }, //利用构造函数返回一个新的LonLat对象 clone:function() { return new OpenLayers.LonLat(this.lon, this.lat); }, //原经纬度对象 + 传入的lon,lat add:function(lon, lat) { if ( (lon == null) || (lat == null) ) { throw new TypeError('LonLat.add cannot receive null values'); } return new OpenLayers.LonLat(this.lon + OpenLayers.Util.toFloat(lon), this.lat + OpenLayers.Util.toFloat(lat)); }, //比较lnglat, //不过四个值 isNaN全true也判定为相当不太理解, 琢磨半天也没想明白 equals:function(ll) { var equals = false; if (ll != null) { equals = ((this.lon == ll.lon && this.lat == ll.lat) || (isNaN(this.lon) && isNaN(this.lat) && isNaN(ll.lon) && isNaN(ll.lat))); } return equals; }, //把当前的经纬度, 从原坐标系转成目标坐标系 transform: function(source, dest) { var point = OpenLayers.Projection.transform( {'x': this.lon, 'y': this.lat}, source, dest); this.lon = point.x; this.lat = point.y; return this; }, //根据换日线 计算出新的经纬度对象 //这个问了一下才明白: 比如画线时候, 鼠标越过换日线 就自动跳回本区间内了, 这时需要自动计算对应点 wrapDateLine: function(maxExtent) { var newLonLat = this.clone(); if (maxExtent) { //shift right? while (newLonLat.lon < maxExtent.left) { newLonLat.lon += maxExtent.getWidth(); } //shift left? while (newLonLat.lon > maxExtent.right) { newLonLat.lon -= maxExtent.getWidth(); } } return newLonLat; }, CLASS_NAME: "OpenLayers.LonLat"}); //从传参str解析经纬度, 新建lonlat对象 OpenLayers.LonLat.fromString = function(str) { var pair = str.split(","); return new OpenLayers.LonLat(pair[0], pair[1]);}; //从数组解析经纬度, 返回新对象OpenLayers.LonLat.fromArray = function(arr) { var gotArr = OpenLayers.Util.isArray(arr), lon = gotArr && arr[0], lat = gotArr && arr[1]; return new OpenLayers.LonLat(lon, lat);}; 三: 屏幕上像素位置类123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960//屏幕上像素位置类OpenLayers.Pixel = OpenLayers.Class({ x: 0.0, y: 0.0, //构造 initialize: function(x, y) { this.x = parseFloat(x); this.y = parseFloat(y); }, //toString带描述 toString:function() { return ("x=" + this.x + ",y=" + this.y); }, //克隆 clone:function() { return new OpenLayers.Pixel(this.x, this.y); }, //判断传入值和当前对象的px全等 equals:function(px) { var equals = false; if (px != null) { equals = ((this.x == px.x && this.y == px.y) || (isNaN(this.x) && isNaN(this.y) && isNaN(px.x) && isNaN(px.y))); } return equals; }, //计算两点距离(直角三角) distanceTo:function(px) { return Math.sqrt( Math.pow(this.x - px.x, 2) + Math.pow(this.y - px.y, 2) ); }, //坐标分别相加, 返回新Px对象 add:function(x, y) { if ( (x == null) || (y == null) ) { throw new TypeError('Pixel.add cannot receive null values'); } return new OpenLayers.Pixel(this.x + x, this.y + y); }, //返回一个偏移后的新Px对象 offset:function(px) { var newPx = this.clone(); if (px) { newPx = this.add(px.x, px.y); } return newPx; }, CLASS_NAME: "OpenLayers.Pixel"}); 四: 表示宽高的类, 没有什么方法1234567891011121314151617181920212223242526272829303132333435//表示宽高的类, 没有什么方法 OpenLayers.Size = OpenLayers.Class({ w: 0.0, h: 0.0, //构造 initialize: function(w, h) { this.w = parseFloat(w); this.h = parseFloat(h); }, //带描述的toString toString:function() { return ("w=" + this.w + ",h=" + this.h); }, //克隆, 返回一个新的 clone:function() { return new OpenLayers.Size(this.w, this.h); }, //判断相等 equals:function(sz) { var equals = false; if (sz != null) { equals = ((this.w == sz.w && this.h == sz.h) || (isNaN(this.w) && isNaN(this.h) && isNaN(sz.w) && isNaN(sz.h))); } return equals; }, CLASS_NAME: "OpenLayers.Size"});