基于OpenLayers的地图封装Javascript类定义
MapCwgisClass.js
//web地图封装类 //MapCwgisClass类的定义 //vp:hsg,myk //create date:2013-07-26 //类 原型定义 var Person = function () { this.m_Name = "good"; } Person.prototype = { m_Name: ‘‘, getName: function () { return this.m_Name; } } //类 原型定义 var MapCwgisClass=function() { //OpenLayers环境参数 OpenLayers.IMAGE_RELOAD_ATTEMPTS = 5; OpenLayers.DOTS_PER_INCH = 25.4 / 0.28; OpenLayers.INCHES_PER_UNIT["千米"] = OpenLayers.INCHES_PER_UNIT["km"]; OpenLayers.INCHES_PER_UNIT["米"] = OpenLayers.INCHES_PER_UNIT["m"]; OpenLayers.INCHES_PER_UNIT["英里"] = OpenLayers.INCHES_PER_UNIT["mi"]; OpenLayers.INCHES_PER_UNIT["英寸"] = OpenLayers.INCHES_PER_UNIT["ft"]; this.defaultFormat = ‘image/png‘; // this.map=null; } MapCwgisClass.prototype={ //属性 wms_url:null, wms_url_getFeature:null, map_divid:null, map: null, currentClientPoint: null, currentClientMapPoint:null, m_CurrentTool:null, ProxyHost:null, defaultFormat:null, defaultProjection:null, defaultBounds:null, vlayer_drawFeature:null, layer_Highlight:null, LayerList:null, //自定义的wms图层属性 map_Raster:null, map_Cadastra:null, // //图层名称 layer_DOM: "RFS_DOM", //DOM层名; layer_XZQ: "RFS_XZQ", //行政区层名 layer_DX : "RFS_MZDW", //其他地类层名 layer_ZD : "RFS_CBD_ZD", //宗地层名 //新添加图层 layer_ZD_LS:"RFS_CBD_LS", //地块历鸣图层名称 //方法 //初始化地图方法 init:function() { //OpenLayers.ProxyHost = ‘GeoServerProxy.aspx?URL=‘; OpenLayers.ProxyHost =this.ProxyHost; // //默认参数 this.defaultProjection = "EPSG:2358"; this.defaultBounds = new OpenLayers.Bounds( 34602000.15, 3318000.3500000006, 34609999.65, 3326999.8500000006); var bounds = this.defaultBounds; //选项 var options = { controls: [], //若添加编辑框就在[]中加上 new OpenLayers.Control.EditingToolbar(vlayer) maxExtent: bounds, maxResolution: 35.154296875, projection: this.defaultProjection, units: ‘m‘, allOverlays: false //true代表允许叠加显示图层 }; //初始化地图对象 this.map = new OpenLayers.Map(this.map_divid, options); map=this.map; //给全局变量map赋值 mapWrap = this; //给全局变量赋值 var t_LLPoint=new OpenLayers.LonLat(0, 0); //this.setCenterPoint(t_LLPoint, 3); //放大到指定范围地图 //this.map.zoomToExtent(bounds); // }, setCenterPoint:function(openLayerLonLatPoint,Level) { if(this.map!=null) { this.map.setCenter(openLayerLonLatPoint,Level); } }, //创建wms图层 createLayerWms:function(p_mapName,p_wms_url,p_layers) { var t_mapLayer = new OpenLayers.Layer.WMS( p_mapName, p_wms_url, { layers: p_layers, styles: ‘‘, tiled: true, srs: defaultProjection, format: defaultFormat }, { singleTile: true, ratio: 1 } ); return t_mapLayer; }, //创建Vector图层 createLayerVector:function(p_LayerName) { var t_layer_vector = new OpenLayers.Layer.Vector(p_LayerName); return t_layer_vector; }, //创建查询图层 createLayerQuery:function(p_QueryLayerName) { var t_querylayerName=‘查询图层‘; if(p_QueryLayerName!=null && p_QueryLayerName!="") { t_querylayerName=p_QueryLayerName; } var t_styleMap=new OpenLayers.StyleMap({ "default": { strokeColor: "#FF0000", strokeOpacity: 1, fillColor: "#6699ff", fillOpacity: 0.2 } }); var t_layer_Query = new OpenLayers.Layer.Vector(t_querylayerName, t_styleMap); return t_layer_Query; }, //添加图层 addLayer:function(pLayer) { if(this.map!=null) { this.map.addLayers([pLayer]); } }, //添加图层集 addLayers:function(pLayers) { if(this.map!=null) { this.map.addLayers([pLayers]); } }, //添加地图系统常用工具控件 //添加系统命令 控件 OK addSystemCommands:function() { // this.map.addControl(new OpenLayers.Control.LayerSwitcher()); //添加比例尺线 var t_scaleline=new OpenLayers.Control.ScaleLine({ topOutUnits: "千米", topInUnits: "米", bottomOutUnits: "英里", bottomInUnits: "英寸" });t_scaleline.setMap(this.map); this.map.addControl(t_scaleline); //添加比例尺 var t_scale=new OpenLayers.Control.Scale();t_scale.setMap(this.map); this.map.addControl(t_scale); //添加鼠标点 var t_mouseposition=new OpenLayers.Control.MousePosition(); t_mouseposition.setMap(this.map); this.map.addControl(t_mouseposition); //添加鹰眼图 var t_ovmap=new OpenLayers.Control.LTOverviewMap(); t_ovmap.setMap(this.map); this.map.addControl(t_ovmap); //添加Zomm动画 var t_zommAnimation=new OpenLayers.Control.LTZommAnimation(); t_zommAnimation.setMap(this.map); this.map.addControl(t_zommAnimation); //添加平移缩放条 var t_panZoomBar=new OpenLayers.Control.PanZoomBar({ position: new OpenLayers.Pixel(2, 15) }); t_panZoomBar.setMap(this.map); this.map.addControl(t_panZoomBar); //添加导航 var t_navigation=new OpenLayers.Control.Navigation(); t_navigation.setMap(this.map); this.map.addControl(t_navigation); }, addSystemCommands_normal:function() { //添加鼠标点 var t_mouseposition=new OpenLayers.Control.MousePosition(); t_mouseposition.setMap(this.map); this.map.addControl(t_mouseposition); //添加Zomm动画 var t_zommAnimation=new OpenLayers.Control.LTZommAnimation(); t_zommAnimation.setMap(this.map); this.map.addControl(t_zommAnimation); }, //添加测量命令 控件 addMeasureCommands:function() { //测量控制操作 var sketchSymbolizers = { "Point": { graphicName: "cross", graphicHeight: 8, graphicWidth: 8, fillColor: "#FF0000", fillOpacity: 1, strokeWidth: 0, }, "Line": { strokeWidth: 1.5, strokeOpacity: 1, strokeColor: "#0000FF" }, "Polygon": { strokeWidth: 1.5, strokeOpacity: 1, strokeColor: "#0000FF", fillColor: "#ccccff", fillOpacity: 0.3 } }; var style = new OpenLayers.Style(); style.addRules([new OpenLayers.Rule({ symbolizer: sketchSymbolizers })]); var styleMap = new OpenLayers.StyleMap({ "default": style }); var renderer = OpenLayers.Util.getParameters(window.location.href).renderer; renderer = (renderer) ? [renderer] : OpenLayers.Layer.Vector.prototype.renderers; var options = { persist: true, handlerOptions: { style: "default", layerOptions: { renderers: renderer, styleMap: styleMap } } }; //线,面测量 measureControls = { line: new OpenLayers.Control.Measure(OpenLayers.Handler.Path, options), polygon: new OpenLayers.Control.Measure(OpenLayers.Handler.Polygon, options) }; var control; for (var key in measureControls) { control = measureControls[key]; control.events.on({ "measure": handleMeasure, //测量距离完成的时候 "measurepartial": handleMeasurePartial //测量距离加入一个点的时候 }); this.map.addControl(control); this.LayerList.add(control); } }, //添加绘制要素的命令 点,线,面 addDrawFeatureCommands:function() { this.vlayer_drawFeature = new OpenLayers.Layer.Vector(); this.map.addLayer(vlayer_drawFeature); drawControls = { point: new OpenLayers.Control.DrawFeature( this.vlayer_drawFeature, OpenLayers.Handler.Point), line: new OpenLayers.Control.DrawFeature( this.vlayer_drawFeature, OpenLayers.Handler.Path), polygon: new OpenLayers.Control.DrawFeature( this.vlayer_drawFeature, OpenLayers.Handler.Polygon) }; for (var key in drawControls) { this.map.addControl(drawControls[key]); this.LayerList.add(drawControls[key]); } }, //添加面板 addPanel:function() { var panel = new OpenLayers.Control.Panel(); this.map.addControls(panel); addPanelControls = { zoomBox: new OpenLayers.Control.ZoomBox() }; for (var key in addPanelControls) { this.map.addControl(addPanelControls[key]); } }, //缩放到要素 zoomToFeatures:function(features) { if(features==null||features==‘undefined‘) return; var left=0; var top=0; var right=0; var bottom=0; // for(i=0;i<features.length;i++) { var fea = features[i]; if(fea.geometry!=null) { var geoBound = fea.geometry.getBounds(); if(geoBound.left<left || left==0) left = geoBound.left; if(geoBound.top>top || top==0) top = geoBound.top; if(geoBound.right>right ||right==0) right = geoBound.right; if(geoBound.bottom<bottom||bottom==0) bottom =geoBound.bottom; } } var zoomBounds = new OpenLayers.Bounds( left, bottom, right, top); this.map.zoomTo(7); var t_LLpoint=this.getFeatureCenterLonLat(features[0]); this.map.panTo(t_LLpoint); }, ////////////////////////// //统一获取要素中心点函数// ////////////////////////// getFeatureCenterLonLat:function (fea){ if(fea==null||fea==‘undefined‘) { return null; } var bound = fea.geometry.getBounds(); var lon = (parseFloat(bound.right) - parseFloat(bound.left)) / 2 + parseFloat(bound.left); var lat = (parseFloat(bound.top ) - parseFloat(bound.bottom)) / 2 + parseFloat(bound.bottom); var lonlat = new OpenLayers.LonLat(lon,lat); return lonlat; },/**/ //长度单位、面积单位汉化 UnitsToCHS:function (units) { var unitsCHS = units; switch (units.toLowerCase()) { case "m": unitsCHS = "米"; break; case "km": unitsCHS = "千米"; break; } return unitsCHS; }, //当前正在执行的工具 getCurrentTool:function() { return m_CurrentTool; }, setCurrentTool:function(p_tool) { if(m_CurrentTool!=null) { try { m_CurrentTool.deactivate(); } catch(e){} } m_CurrentTool=p_tool; try { m_CurrentTool.activate(); } catch(e){} }, CLASS_NAME: "MapCwgisClass" }; //设置全局变量和初始化地图控件 //============================================================ var t_p = new Person(); var t_t=t_p.getName(); //定义全局变量 var map=null; var mapWrap=null; //页面地图加载 //给全局变量赋值 mapWrap = new MapCwgisClass(); mapWrap.wms_url = wms_url; mapWrap.wms_url_getFeature = wms_url_getFeature; mapWrap.map_divid = ‘map‘; mapWrap.ProxyHost = ‘GeoServerProxy.aspx?URL=‘; mapWrap.init(); mapWrap.addSystemCommands(); //加载常用的系统功能 //============================================================