Vue+Openlayers+Draw实现画笔切换功能,切换画笔为点、线、面
場(chǎng)景
Vue+Openlayer使用Draw實(shí)現(xiàn)交互式繪制線段:
Vue+Openlayer使用Draw實(shí)現(xiàn)交互式繪制線段_霸道流氓氣質(zhì)的博客-CSDN博客
Vue+Openlayer使用Draw實(shí)現(xiàn)交互式繪制多邊形并獲取面積:
Vue+Openlayer使用Draw實(shí)現(xiàn)交互式繪制多邊形并獲取面積_霸道流氓氣質(zhì)的博客-CSDN博客
如果需要將繪制點(diǎn)、線、面集成在一個(gè)頁(yè)面中并且可以實(shí)現(xiàn)切換畫筆樣式。
?
注:
博客:
霸道流氓氣質(zhì)的博客_CSDN博客-C#,架構(gòu)之路,SpringBoot領(lǐng)域博主
關(guān)注公眾號(hào)
霸道的程序猿
獲取編程相關(guān)電子書、教程推送與免費(fèi)下載。
實(shí)現(xiàn)
1、頁(yè)面上添加el-radio-group用來(lái)選擇切換畫筆
??????????? <el-radio-group v-model="radio" @change="toolChange"><el-radio? label="1">普通鼠標(biāo)</el-radio><el-radio? label="2">繪制線</el-radio><el-radio? label="3">繪制區(qū)域</el-radio>?????<el-radio? label="4">繪制點(diǎn)</el-radio>?????</el-radio-group>2、綁定的model值聲明
??????? data() {return {?????????radio: '1',selectedStyle:null,//畫筆stylecurrentTool:null,};3、頁(yè)面mounted中初始化地圖時(shí)設(shè)置坐標(biāo)的選中樣式等。
??????? methods: {????//初始化地圖init() {let self = this;// 獲取點(diǎn)擊地圖的坐標(biāo)(選中樣式)self.selectedStyle = new Style({fill: new Fill({color: 'rgba(1, 210, 241, 0.2)'}),stroke: new Stroke({color: 'yellow',width: 4})});// 選擇線的工具類this.selectTool = new Select({multi: true,hitTolerance: 10, // 誤差style: this.selectedStyle // 選中要素的樣式})4、改變畫筆的change事件
??????????? //改變畫筆toolChange(val){let self = this;//移除交互self.map.removeInteraction(self.selectTool);self.coordinate = []//清空交互的圖層self.drawLineLayer.getSource().clear()self.removeDraw();//添加交互self.map.addInteraction(self.selectTool)switch(val){????????????case '2'://調(diào)用繪圖工具并傳遞類型為線,其他類型有Point,LineString,Polygon,Circleself.onAddInteraction('LineString')self.currentTool = "LineString";break;case '3'://調(diào)用繪圖工具并傳遞類型為線,其他類型有Point,LineString,Polygon,Circleself.onAddInteraction('Polygon')self.currentTool = "Polygon";break;case '4'://調(diào)用繪圖工具并傳遞類型為線,其他類型有Point,LineString,Polygon,Circleself.onAddInteraction('Point')self.currentTool = "Point";break;}},重新移除交互并清空?qǐng)D層與點(diǎn)位數(shù)據(jù)存儲(chǔ)點(diǎn),然后調(diào)用添加繪圖工具的方法并傳遞不同的參數(shù)類型,并將選中的畫筆類型記錄下來(lái)。
添加繪圖工具的方法實(shí)現(xiàn)
??????????? // 繪圖工具onAddInteraction(type) {let self = this//勾繪矢量圖形的類this.draw = new Draw({//source代表勾繪的要素屬于的數(shù)據(jù)集source: self.drawLineSource,//type 表示勾繪的要素包含的 geometry 類型type: type})//繪制結(jié)束時(shí)觸發(fā)的事件this.draw.on('drawend', function(e) {?????????????const geometry = e.feature.getGeometry()let pointArr = geometry.getCoordinates()debugger//限制只繪制一個(gè)多邊形self.removeDraw()switch(self.currentTool){????????????case 'LineString':self.pointArr = pointArr;//調(diào)用接口保存線let param = {pointList:self.pointArr}insertLineRequest(param).then((response) => {self.msgSuccess("新增成功");//刷新監(jiān)測(cè)點(diǎn)數(shù)據(jù)self.getPointList();//成功之后刪除線self.drawLineLayer.getSource().clear()});break;case 'Polygon'://只獲取第一個(gè)多邊形的坐標(biāo)???self.pointArr = pointArr[0];self.dialogVisible = true;break;case 'Point'://調(diào)用新增點(diǎn)的接口self.pointArr = pointArr;break;}????????????????????????????})self.map.addInteraction(this.draw)},這樣就可以根據(jù)不同的畫筆類型在繪制結(jié)束時(shí)進(jìn)行不同的業(yè)務(wù)處理
其中刪除draw的方法
??????????? //刪除交互removeDraw() {this.map.removeInteraction(this.draw)},5、頁(yè)面上添加重新繪制按鈕,其點(diǎn)擊事件中
??????????? //重新繪制clear() {this.coordinate = []this.drawLineLayer.getSource().clear()//添加交互this.map.addInteraction(this.selectTool)switch(this.currentTool){????????????case 'LineString':this.onAddInteraction('LineString')break;case 'Polygon':this.onAddInteraction('Polygon')break;case 'Point':this.onAddInteraction('Point')break;}??????????????????????????},根據(jù)當(dāng)前選中的畫筆類型,初始化畫筆為對(duì)應(yīng)的類型。
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的Vue+Openlayers+Draw实现画笔切换功能,切换画笔为点、线、面的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CSS中使用flex弹性布局实现上下左右
- 下一篇: Leaflet中使用NavBar插件实现