04测距、测面积ToolMeasure.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>测距、测面积</title>
  7. <link rel="stylesheet" href="../../dist/index.css" />
  8. <script src="../assets/vue@2.7.14.js"></script>
  9. <script src="../../dist/index.js"></script>
  10. <style>
  11. body {
  12. margin: 0
  13. }
  14. #map {
  15. height: 100vh;
  16. }
  17. .control {
  18. position: absolute;
  19. top: 20px;
  20. left: 40px;
  21. padding: 10px 10px 0;
  22. background: #fff;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="app">
  28. <div id="map"></div>
  29. <div class="control">
  30. <button @click="initDefault">初始化测距(开始提示,右键撤销)</button>
  31. <button @click="initRed">初始化测距(红色,右键结束)</button>
  32. <button @click="initArea">初始化测面积(右键结束)</button>
  33. <button @click="toggleTooltip">隐藏/显示结果提示(单个)</button>
  34. <button @click="toggleAllTooltip">隐藏/显示结果提示(全部)</button>
  35. <button @click="_ => measure.stop()">停止</button>
  36. <button @click="start">开始</button>
  37. <button @click="_ => measure.clear()">全部清除</button>
  38. <button @click="destroy">销毁</button>
  39. <p>注意:需要导入样式文件 dist/index.css </p>
  40. </div>
  41. </div>
  42. </body>
  43. <script>
  44. new Vue({
  45. el: '#app',
  46. data () {
  47. this.showTooltip = true
  48. this.showAllTooltip = true
  49. return {
  50. viewShedVisible: true
  51. }
  52. },
  53. mounted () {
  54. this.map = new CTMapOl.extend.InitMap({
  55. domId: 'map'
  56. })
  57. },
  58. methods: {
  59. initMeasure (options) {
  60. if (this.measure) {
  61. this.measure.destroy()
  62. }
  63. // 不要在 data 中定义 measure 变量,无需响应式
  64. this.measure = new CTMapOl.extend.ToolMeasure(this.map.map, options)
  65. },
  66. initDefault () {
  67. this.initMeasure({ startTip: '单击确定起点' })
  68. this.start()
  69. },
  70. initRed () {
  71. this.initMeasure({
  72. color: 'red',
  73. rightClickAction: 'finish',
  74. onFinish: result => {
  75. this.lastMeasure = result.feature
  76. console.log(result)
  77. },
  78. onReduce: result => {
  79. console.log('删除点后', result)
  80. },
  81. onRemove: feature => {
  82. console.log('删除测量', feature)
  83. }
  84. })
  85. this.start()
  86. },
  87. initArea () {
  88. this.initMeasure({ type: 'area', rightClickAction: 'finish' })
  89. this.start()
  90. },
  91. toggleTooltip () {
  92. if (!this.lastMeasure) { return }
  93. this.showTooltip = !this.showTooltip
  94. this.measure.showTooltip(this.showTooltip, this.lastMeasure)
  95. },
  96. toggleAllTooltip () {
  97. this.showAllTooltip = !this.showAllTooltip
  98. this.measure.showTooltip(this.showAllTooltip)
  99. },
  100. start () {
  101. if (this.measure) {
  102. this.measure.start()
  103. } else {
  104. alert('必须先初始化')
  105. }
  106. },
  107. destroy () {
  108. this.measure.destroy()
  109. this.measure = null
  110. }
  111. }
  112. })
  113. </script>
  114. </html>