本文实例为大家分享了openlayers4.6.5实现距离量测和面积量测的具体代码,供大家参考,具体内容如下
版本: openlayers4.6.5
效果图:
小插曲:
原本使用ol官方提供的 量测例子,就挺不错的。但是由于放在项目中后。量测样式不知道为啥出不来,找了半天原因 也没有找到,单独在一个html中完全没问题。所以推测可能和项目中哪些地方有冲突,但是问题暂时没找出来,项目也比较急,所以只能自己实现文字标注部门的样式,实现效果如上图gif所示。
实现原理:
量测功能还是使用了ol例子提供的源码,修改部分主要是在标注这一块,另外就是时刻去添加这个标注 然后时刻删除这个标注 就可以了。
完整的js代码如下(鼠标样式图标 我没放上来,有需要的我给你发邮箱):
var draw; var click=false; var output=0; var vector; var source; var lastPolygonLabelFeature;//记录上一个面标注要素 var lastLengthLabelFeature;//记录上一个点标注要素 $( function(){ $("#measureDistance").click(function(){ if(draw){ map.removeInteraction(draw); } addInteraction("length"); setMeasureCur(); }) $("#measureArea").click(function(){ if(draw){ map.removeInteraction(draw); } addInteraction("area"); setMeasureCur(); }) $("#measureClear").click(function(){ map.removeInteraction(draw); vector.setSource(null); source=new ol.source.Vector(); vector.setSource(source); lastPolygonLabelFeature=null; lastLengthLabelFeature=null; click=false; sketch = null; output="0"; reSetCur(); }) function setMeasureCur(){ $('#map').css({ cursor:"url(../../static/images/measureIcon/measure.cur), auto" }); } function reSetCur(){ $('#map').css('cursor','default'); } source = new ol.source.Vector(); vector = new ol.layer.Vector({ source: source, style: new ol.style.Style({ fill: new ol.style.Fill({//面的填充颜色 color: 'rgba(255, 0, 0, 0.1)' }), stroke: new ol.style.Stroke({ color: 'rgb(255,116,3)', width: 2 }), image: new ol.style.Circle({ radius: 3, stroke: new ol.style.Stroke({ color: 'rgba(255, 0, 0,1)', width: 2 }), fill: new ol.style.Fill({ color: 'rgba(255,255,255)' }) }) }) }); map.addLayer(vector); var sketch; var pointerMoveHandler = function(evt) { if (evt.dragging) { return; } var Coord; if(sketch){ var geom = sketch.getGeometry(); if (geom instanceof ol.geom.Polygon) { if(lastPolygonLabelFeature){ //鼠标移动 不停的添加和删除 source.removeFeature(lastPolygonLabelFeature); } Coord = geom.getInteriorPoint().getCoordinates(); //新建一个要素ol.Feature var newFeature = new ol.Feature({ geometry: new ol.geom.Point(Coord), //几何信息 name: output }); lastPolygonLabelFeature=newFeature; newFeature.setStyle(createLabelStyle(newFeature,0,0)); } else if (geom instanceof ol.geom.LineString) { if(lastLengthLabelFeature){ source.removeFeature(lastLengthLabelFeature); } Coord = geom.getLastCoordinate(); //新建一个要素ol.Feature var newFeature = new ol.Feature({ geometry: new ol.geom.Point(Coord), //几何信息 name: output }); lastLengthLabelFeature=newFeature; newFeature.setStyle(createLabelStyle(newFeature,35,-10)); } //设置要素样式 source.addFeature(newFeature); } }; map.on('pointermove', pointerMoveHandler); map.on('click', function(evt){ var coordinate = evt.coordinate; //鼠标单击点的坐标 console.log(coordinate); if(output=="0"){ lastPolygonLabelFeature=null; if(lastLengthLabelFeature){ source.removeFeature(lastLengthLabelFeature); lastLengthLabelFeature=null; } return; } var Coord; if(sketch){ var geom = sketch.getGeometry(); if (geom instanceof ol.geom.Polygon) { if(lastPolygonLabelFeature){ source.removeFeature(lastPolygonLabelFeature); } Coord = geom.getInteriorPoint().getCoordinates(); //新建一个要素ol.Feature var newFeature = new ol.Feature({ geometry: new ol.geom.Point(Coord), //几何信息 name: output }); lastPolygonLabelFeature=newFeature; newFeature.setStyle(createLabelStyle(newFeature,0,0)); //设置要素样式 source.addFeature(newFeature); } else if (geom instanceof ol.geom.LineString) { Coord = geom.getLastCoordinate(); //新建一个要素ol.Feature var newFeature = new ol.Feature({ geometry: new ol.geom.Point(Coord), //几何信息 name: output }); newFeature.setStyle(createLabelStyle(newFeature,35,-10)); //设置要素样式 source.addFeature(newFeature); } var pointFeature = new ol.Feature({ geometry: new ol.geom.Point(coordinate), //几何信息 name: output }); source.addFeature(pointFeature); } }); //矢量标注样式设置函数,设置image为图标ol.style.Icon function createLabelStyle(feature,offsetX,offsetY){ return new ol.style.Style({ // image: new ol.style.Icon({ // anchor: [0.5, 60], //锚点 // anchorOrigin:'top-right', //锚点源 // anchorXUnits: 'fraction', //锚点X值单位 // anchorYUnits: 'pixels', //锚点Y值单位 // offsetOrigin: 'top-right', //偏移原点 // opacity: 0.75, // src: 'OL3Demo/images/label/blueIcon.png' //图标的URL // }), text: new ol.style.Text({ textAlign: 'center', //位置 textBaseline: 'middle', //基准线 font: 'normal 10px sans-serif', //文字样式 text: feature.get('name'), //文本内容 fill: new ol.style.Fill({ //文本填充样式(即文字颜色) color: 'white' }), stroke: new ol.style.Stroke({ color: 'black', width: 5 }), offsetX:offsetX, offsetY:offsetY }) }); } function addInteraction(drawType) { var type = (drawType== 'area' "0"; }, this); } var formatLength = function(line) { var length = ol.Sphere.getLength(line); var output; if (length > 100) { output = (Math.round(length / 1000 * 100) / 100) + ' ' + '千米'; } else { output = (Math.round(length * 100) / 100) + ' ' + '米'; } return output; }; var formatArea = function(polygon) { var area = ol.Sphere.getArea(polygon); var output; if (area > 10000) { output = (Math.round(area / 1000000 * 100) / 100) + ' ' + '平方千米'; } else { output = (Math.round(area * 100) / 100) + ' ' + '平方米'; } return output; }; })
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]