06图形绘制、周边分析.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. <script src="../assets/vue@2.7.14.js"></script>
  8. <script src="../../dist/index.js"></script>
  9. <style>
  10. body {
  11. margin: 0
  12. }
  13. #map {
  14. height: 100vh;
  15. }
  16. .control {
  17. position: absolute;
  18. top: 20px;
  19. left: 40px;
  20. padding: 10px;
  21. background: #fff;
  22. }
  23. .control button {
  24. min-width: 3em;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="app">
  30. <div id="map"></div>
  31. <div class="control">
  32. <template v-if="!ready">
  33. <button @click="init">初始化</button>
  34. </template>
  35. <template v-else>
  36. <button @click="startPoint">点</button>
  37. <button @click="_ => buffer.start('lineString', +radius)">线</button>
  38. <button @click="startLine">线(自定义样式)</button>
  39. <button @click="_ => buffer.start('circle', +radius)">圆(不支持周边分析)</button>
  40. <button @click="_ => buffer.start('box', +radius)">矩形</button>
  41. <button @click="_ => buffer.start('polygon', +radius)">一般多边形(周边分析不支持自交叉多边形)</button>
  42. <button @click="_ => buffer.undo(false)">撤销上一步</button>
  43. <button @click="_ => buffer.stop()">停止绘制</button>
  44. <button @click="loadFromData">加载图形并分析周边</button>
  45. <button @click="_ => buffer.clear()">清空</button>
  46. <button @click="destroy">销毁</button>
  47. <br>
  48. <button @click="setBufferLimit">已{{limit? '启用' : '禁用'}}缓冲区范围限制</button>
  49. <span>周边缓冲距离: {{radius}}km</span>
  50. <span style="margin-left: 2em;">
  51. 0km
  52. <input type="range" min="0" :max="max" v-model="radius" @input="changeRadius">
  53. <input type="number" v-model="max" style="width: 50px"> km
  54. </span>
  55. <br>
  56. <br>
  57. <span>提示: 缓冲距离设置为0即为图形绘制功能,使用 onAddPoint 回调时,自动禁用默认缓冲距离限制</span>
  58. </template>
  59. </div>
  60. </div>
  61. </body>
  62. <script>
  63. const { Style, Fill, Stroke } = CTMapOl.style
  64. new Vue({
  65. el: '#app',
  66. data () {
  67. return {
  68. longitude: '',
  69. latitude: '',
  70. radius: 1,
  71. limit: true,
  72. max: 10,
  73. ready: false
  74. }
  75. },
  76. watch: {
  77. max (val) {
  78. if (val < this.radius) {
  79. this.radius = val
  80. this.changeRadius()
  81. }
  82. this.buffer.setBufferMax(val)
  83. }
  84. },
  85. mounted () {
  86. this.map = new CTMapOl.extend.InitMap({
  87. domId: 'map'
  88. })
  89. },
  90. methods: {
  91. init () {
  92. // 不要在 data 中定义 buffer 变量,无需响应式
  93. this.buffer = new CTMapOl.extend.ToolBuffer(this.map.map, {
  94. onPointerMove: info => {
  95. console.log('鼠标移动', info)
  96. },
  97. onAddPoint: info => {
  98. console.log('增加一个点', info)
  99. this.limit = false // 启用 onAddPoint 回调时,会自动禁用默认缓冲限制,不然可能有冲突
  100. if (info.length > 1000 || info.area > 1000000) {
  101. // this.buffer.undo() // 超出撤销
  102. }
  103. }
  104. }, result => {
  105. // result.origin 为原始图形geojson
  106. // result.buffer 缓冲区geojson
  107. // result.center 缓冲区覆盖圆 中心点
  108. // result.radius 缓冲区覆盖圆 半径
  109. console.log(result)
  110. }, _ => {
  111. console.error('范围超出限制')
  112. })
  113. this.ready = true
  114. },
  115. startPoint () {
  116. const bufferStyle = CTMapOl.extend.style.radialGradientCircle({
  117. map: this.map.map,
  118. colorStops: [
  119. [0, 'rgba(255,0,0,0)'],
  120. [0.85, 'rgba(255,0,0,0.2)'],
  121. [1, 'rgba(255,0,0,0.5)']
  122. ],
  123. strokeStyle: 'rgba(255,0,0,1)'
  124. })
  125. this.buffer.start('point', +this.radius, {
  126. bufferStyle
  127. })
  128. },
  129. startLine () {
  130. const lineStyle = new Style({
  131. stroke: new Stroke({
  132. width: 4,
  133. color: '#00ff00'
  134. })
  135. })
  136. const bufferStyle = new Style({
  137. fill: new Fill({
  138. color: 'rgba(92,255,0,0.3)'
  139. })
  140. })
  141. this.buffer.start('lineString', +this.radius, {
  142. originStyle: lineStyle,
  143. bufferStyle: bufferStyle
  144. })
  145. },
  146. loadFromData () {
  147. const coordinates = [[116.39, 39.91], [116.40, 39.91], [116.39, 39.92], [116.39, 39.91]]
  148. const geom = new CTMapOl.geom.Polygon([coordinates])
  149. geom.transform('EPSG:4326', 'EPSG:3857')
  150. this.buffer.loadFromData({
  151. geometry: geom,
  152. geoJson: null, // 也可以加载geoJson
  153. radius: 3,
  154. originStyle: new Style({
  155. stroke: new Stroke({ width: 3, color: '#00ffe9' }),
  156. fill: new Fill({ color: 'rgba(0,255,233,0.3)' })
  157. }),
  158. bufferStyle: new Style({
  159. fill: new Fill({ color: 'rgba(0,255,233,0.3)' })
  160. })
  161. })
  162. },
  163. setBufferLimit () {
  164. this.limit = !this.limit
  165. this.limit = this.buffer.setBufferLimit(this.limit)
  166. },
  167. changeRadius () {
  168. this.buffer.changeRadius(+this.radius)
  169. },
  170. destroy () {
  171. this.buffer.destroy()
  172. this.buffer = null
  173. this.ready = false
  174. }
  175. }
  176. })
  177. </script>
  178. </html>