当前位置:   article > 正文

OpenLayers6(6):绘制图形工具条封装(Draw、Snap、Modify)_openlayers snap

openlayers snap

1 版本

  • OpenLayers:6.14.1


2 说明

  • Draw:绘制图形
  • Snap:捕捉图形
  • Modify:修改图形

3 绘制图形组件

将绘制界面直接封装成vue单文件组件,上代码:

  1. <template>
  2. <div class="ol-draw-collapse">
  3. <el-collapse :value="['1']">
  4. <el-collapse-item name="12">
  5. <template slot="title">
  6. <p-button class="t-btn" tip="图形绘制" icon="draw" :place="pPlace" />
  7. </template>
  8. <p-button tip="点绘制" icon="point" :place="pPlace" @click="startDraw('Point')" />
  9. <p-button tip="线绘制" icon="polyline" :place="pPlace" @click="startDraw('LineString')" />
  10. <p-button tip="面绘制" icon="polygon" :place="pPlace" @click="startDraw('Polygon')" />
  11. <p-button tip="圆绘制" icon="circle" :place="pPlace" @click="startDraw('Circle')" />
  12. <p-button tip="完成绘制" icon="done" :place="pPlace" @click="doneDraw()" />
  13. <p-button tip="清除图形" icon="clear" :place="pPlace" @click="clearDraw()" />
  14. </el-collapse-item>
  15. </el-collapse>
  16. </div>
  17. </template>
  18. <script>
  19. // PButton 自己封装的el-button按钮组件
  20. import PButton from "../common/PButton.vue";
  21. // ODraw 编写的绘制图形的类
  22. import { ODraw } from "@/assets/js/ol/POpenLayers.js";
  23. export default {
  24. components: { PButton },
  25. name: "OlDraw",
  26. props: {
  27. pMap: {
  28. default: null,
  29. require: true,
  30. },
  31. pPlace: {
  32. default: "right",
  33. require: false,
  34. },
  35. },
  36. data() {
  37. return {
  38. mOlDraw: null,
  39. };
  40. },
  41. mounted() {
  42. this.mOlDraw = new ODraw(this.pMap);
  43. this.mOlDraw.init();
  44. },
  45. methods: {
  46. startDraw(drawType) {
  47. this.mOlDraw.startDraw(drawType);
  48. },
  49. doneDraw() {
  50. this.mOlDraw.doneDraw();
  51. },
  52. clearDraw() {
  53. this.mOlDraw.clearDraw();
  54. },
  55. },
  56. };
  57. </script>
  58. <style lang='scss'>
  59. @import "~@/assets/css/mixin.scss";
  60. .ol-draw-collapse {
  61. @include p-collapse;
  62. }
  63. </style>

4 绘制图形类

主要基于动态原型模型编写绘制图形的类ODraw,上代码:

  1. export function ODraw(map) {
  2. let this_ = this;
  3. this_.mMap = map;
  4. this_.mSource = null;
  5. this_.mLayer = null;
  6. this_.mDraw = null;
  7. this_.mModify = null;
  8. this_.mSnap = null;
  9. if (typeof ODraw.initialized == "undefined") {
  10. ODraw.prototype.init = () => {
  11. this_.mSource = new VectorSource();
  12. this_.mLayer = new VectorLayer({
  13. title: "草图图层", // ol-ext图层控件属性
  14. noSwitcherDelete: true, // ol-ext图层控件属性
  15. source: this_.mSource,
  16. });
  17. this_.mMap.addLayer(this_.mLayer);
  18. this_.mModify = new Modify({
  19. source: this_.mSource,
  20. style: measureModifyStyle() // 自定义样式
  21. });
  22. this_.mMap.addInteraction(this_.mModify);
  23. };
  24. ODraw.prototype.addInteractions = (drawType) => {
  25. this_.mDraw = new Draw({
  26. source: this_.mSource,
  27. type: drawType,
  28. });
  29. this_.mDraw.on('drawstart', function () {
  30. this_.mModify.setActive(false);
  31. });
  32. this_.mDraw.on('drawend', function () {
  33. this_.mModify.setActive(true);
  34. });
  35. this_.mModify.setActive(true);
  36. this_.mMap.addInteraction(this_.mDraw);
  37. this_.mSnap = new Snap({
  38. source: this_.mSource
  39. });
  40. this_.mMap.addInteraction(this_.mSnap);
  41. };
  42. ODraw.prototype.removeInteractions = () => {
  43. this_.mDraw && this_.mMap.removeInteraction(this_.mDraw);
  44. this_.mSnap && this_.mMap.removeInteraction(this_.mSnap);
  45. };
  46. ODraw.prototype.startDraw = (drawType) => {
  47. this_.removeInteractions();
  48. this_.addInteractions(drawType);
  49. };
  50. ODraw.prototype.doneDraw = () => {
  51. this_.removeInteractions();
  52. // this_.mMap.getView().fit(this_.mSource.getExtent());
  53. };
  54. ODraw.prototype.clearDraw = () => {
  55. this_.mSource.clear();
  56. };
  57. ODraw.initialized = true;
  58. }
  59. }

5 实现效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/607461
推荐阅读