博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OL2中测量工具的扩展
阅读量:4969 次
发布时间:2019-06-12

本文共 7895 字,大约阅读时间需要 26 分钟。

概述:

本文共享一个扩展后的测量工具,实现绘制时测量结果的实时展示。

效果:

测距

测面

实现:

1、扩展MeasureControl

/** * Class: OpenLayers.Control.MeasureDistance *  * 地图距离量算控件 *  *  * 继承自: *  - 
*//** * @requires OpenLayers/Control/Measure.js */ OpenLayers.Control.MeasureDistance = OpenLayers.Class(OpenLayers.Control.Measure, { /** * Property: type * {String} 工具类型 */ type: OpenLayers.Control.TYPE_TOOL, /** * Property: outputDiv * {String} 量算结果输出DIV */ outputDiv: null, /** * Property: correctParam * 纠正系数 */ correctParam: 1, /** * Property: clicks * {Integer} 鼠标点击次数 */ clicks: null, /** * Property: title * {String} 工具按钮提示文字 */ title: "距离量算", /** * Constructor: OpenLayers.Control.MeasureDistance * 初始化函数 * * Parameters: * handler - {
} * options - {Object} */ initialize: function(options) { OpenLayers.Util.extend(this, options); var style = new OpenLayers.Style(); style.addRules([ new OpenLayers.Rule({symbolizer: measureSymbolizers})]); var styleMap = new OpenLayers.StyleMap({"default": style}); test={layerOptions:{stypeMap:styleMap}}; newArguments = [OpenLayers.Handler.Path, { persist: true, handlerOptions: { layerOptions: {styleMap: styleMap} } }]; OpenLayers.Control.Measure.prototype.initialize.apply(this,newArguments); this.events.on({ measure: this.handleMeasurements, measurepartial: this.handlePartialMeasurements }); var modifyFeature = this.handler.modifyFeature; var scope = this; this.handler.modifyFeature = function(pixel, drawing){ modifyFeature.apply(scope.handler, arguments); }; this.clicks = 0; }, /** * Method: activate * 控件被触发时设置鼠标手势 */ activate: function(){ if(this.map.labelLayer){ this.map.labelLayer.clear(); } else{ this.map.labelLayer = new OpenLayers.Layer.Labels("mesurelabel", "mesurelabelLayer"); this.map.addLayer(this.map.labelLayer); } if(this.map.vectorLayer){ this.map.vectorLayer.removeAllFeatures(); } else{ this.map.vectorLayer = new OpenLayers.Layer.Vector("Vector Layer"); this.map.addLayer(this.map.vectorLayer); } OpenLayers.Control.Measure.prototype.activate.apply(this, arguments); }, /** * Method: activate * 控件被触发时设置鼠标手势 */ deactivate: function(){ OpenLayers.Control.Measure.prototype.deactivate.apply(this, arguments); if(this.map.labelLayer){ this.map.labelLayer.clear(); this.map.vectorLayer.removeAllFeatures(); } }, /** * Method: handleMeasurements * 量算结束 */ handleMeasurements:function(event) { var geometry = event.geometry; var units = event.units; var order = event.order; var measure = event.measure; var out = ""; var point=""; this.handler.deactivate(); var features = [new OpenLayers.Feature.Vector( geometry,{} )]; this.map.vectorLayer.style = measureSymbolizers.Polygon; this.map.vectorLayer.addFeatures(features); if(order == 1) { var length=geometry.components.length; point =geometry.components[length-1]; } else { out += "" + measure.toFixed(3)*this.correctParam + " " + units + "
2
"; var length=geometry.components[0].components.length; point =geometry.components[0].components[length-2]; this.map.labelLayer.setZIndex(this.map.Z_INDEX_BASE["Control"]); var labelDiv = new OpenLayers.Label(new OpenLayers.LonLat(point.x,point.y),out, "MeasureLable"); this.map.labelLayer.add(labelDiv); } $(this.map.labelLayer.div).css("z-index","999"); var closeDiv = new OpenLayers.Label(new OpenLayers.LonLat(point.x,point.y),"×", "MeasureClose"); this.map.labelLayer.add(closeDiv); var scope = this; $(closeDiv.labelDiv).attr("title","关闭"); $(closeDiv.labelDiv).on("click",function(e){ scope.deactivate(); }); }, /** * Method: measurePartial * Called each time a new point is added to the measurement sketch. * * Parameters: * point - {
} The last point added. * geometry - {
} The sketch geometry. */ measurePartial: function(point, geometry) { if (geometry.getLength() > 0) { geometry = geometry.clone(); if(geometry.CLASS_NAME==="OpenLayers.Geometry.LineString"){ var length = geometry.getGeodesicLength(this.map.getProjectionObject()); var out = (length/1000).toFixed(3)*this.correctParam+"km"; var labelDiv = new OpenLayers.Label(new OpenLayers.LonLat(point.x,point.y),out, "MeasureLable"); this.map.labelLayer.add(labelDiv); } this.delayedTrigger = window.setTimeout( OpenLayers.Function.bind(function() { this.measure(geometry, "measurepartial"); }, this), this.partialDelay ); } else{ this.map.labelLayer.clear(); this.map.vectorLayer.removeAllFeatures(); if(geometry.CLASS_NAME==="OpenLayers.Geometry.LineString"){ var out = "起点"; var labelDiv = new OpenLayers.Label(new OpenLayers.LonLat(point.x,point.y),out, "MeasureLable"); this.map.labelLayer.add(labelDiv); } } }, CLASS_NAME: "OpenLayers.Control.MeasureDistance"});/** * Class: OpenLayers.Control.MeasureArea * * 地图面积量算控件 * * * 继承自: * -
*/ OpenLayers.Control.MeasureArea = OpenLayers.Class(OpenLayers.Control.MeasureDistance, { /** * Property: title * {String} 工具按钮提示文字 */ title: "面积量算", /** * Constructor: OpenLayers.Control.MeasureDistance * * Parameters: * handler - {
} * options - {Object} */ initialize: function(options) { var style = new OpenLayers.Style(); style.addRules([ new OpenLayers.Rule({symbolizer: measureSymbolizers})]); var styleMap = new OpenLayers.StyleMap({"default": style}); test={layerOptions:{stypeMap:styleMap}}; newArguments = [OpenLayers.Handler.Polygon, { persist: true, handlerOptions: { layerOptions: {styleMap: styleMap} } }]; OpenLayers.Control.Measure.prototype.initialize.apply(this,newArguments); this.events.on({ measure: this.handleMeasurements, measurepartial: this.handlePartialMeasurements }); var modifyFeature = this.handler.modifyFeature; var scope = this; this.handler.modifyFeature = function(pixel, drawing){ modifyFeature.apply(scope.handler, arguments); } this.clicks = 0; }, /** * Method: activate * 控件被触发时设置鼠标手势 */ activate: function(){ if(this.map.labelLayer){ this.map.labelLayer.clear(); } else{ this.map.labelLayer = new OpenLayers.Layer.Labels("mesurelabel", "mesurelabelLayer"); this.map.addLayer(this.map.labelLayer); } if(this.map.vectorLayer){ this.map.vectorLayer.removeAllFeatures(); } else{ this.map.vectorLayer = new OpenLayers.Layer.Vector("Vector Layer"); this.map.addLayer(this.map.vectorLayer); } OpenLayers.Control.Measure.prototype.activate.apply(this, arguments); }, CLASS_NAME: "OpenLayers.Control.MeasureArea"});//style the sketch fancyvar measureSymbolizers = { "Point": { pointRadius: 3, graphicName: "circle", fillColor: "#8AC4F0", fillOpacity: 1, strokeWidth: 1, strokeOpacity: 1, strokeColor: "#FAD00B" }, "Line": { strokeWidth: 3, strokeOpacity: 1, strokeColor: "#ee9900", strokeDashstyle: "solid" }, "Polygon": { strokeWidth: 3, strokeOpacity: 1, strokeColor: "#ee9900", fillColor: "#66cccc", fillOpacity: 0.3 }};
2、页面调用

    
openlayers map

---------------------------------------------------------------------------------------------------------------

技术博客

http://blog.csdn/gisshixisheng

在线教程

http://edu.csdn/course/detail/799

Github

https://github.com/lzugis/

联系方式

q       q:1004740957

e-mail:niujp08@qq.com

公众号:lzugis15

Q Q 群:452117357(webgis)

             337469080()

转载于:https://www.cnblogs.com/lzugis/p/6539754.html

你可能感兴趣的文章
[转载]java开发中的23种设计模式
查看>>
表格的拖拽功能
查看>>
函数的形参和实参
查看>>
【TP SRM 703 div2 500】 GCDGraph
查看>>
webdriver api
查看>>
apache 实现图标缓存客户端
查看>>
揭秘:黑客必备的Kali Linux是什么,有哪些弊端?
查看>>
linux系统的远程控制方法——学神IT教育
查看>>
springboot+mybatis报错Invalid bound statement (not found)
查看>>
Linux环境下SolrCloud集群环境搭建关键步骤
查看>>
P3565 [POI2014]HOT-Hotels
查看>>
MongoDB的简单使用
查看>>
hdfs 命令使用
查看>>
prometheus配置
查看>>
【noip2004】虫食算——剪枝DFS
查看>>
python 多进程和多线程的区别
查看>>
sigar
查看>>
iOS7自定义statusbar和navigationbar的若干问题
查看>>
[Locked] Wiggle Sort
查看>>
deque
查看>>