08POI标记Markers.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <!DOCTYPE html>
  2. <html lang="zh_CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>POI标记</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 10px 0;
  21. background: #fff;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="app">
  27. <div id="map"></div>
  28. <div class="control">
  29. <button @click="toggleVisible">展示/隐藏标记</button>
  30. <button @click="toggleCluster">{{cluster ? '已启用' : '已禁用'}}聚合</button>
  31. <button @click="setData(false)">增加标记</button>
  32. <button @click="setData(true)">替换标记</button>
  33. <button @click="selectMarker">选中标记</button>
  34. <button @click="deSelect">取消选中(多选模式需传参)</button>
  35. <button @click="selectReverse">反选(仅多选模式)</button>
  36. <button @click="clearSelected">清空选中</button>
  37. <button @click="_ => markers.clearMarkers()">清空标记</button>
  38. <p>注意:Vue项目中<b>不要</b>将地图相关实例对象赋值给响应式变量</p>
  39. </div>
  40. </div>
  41. </body>
  42. <script>
  43. function makeRandomData (count) {
  44. return Array(count).fill('').map((item, index) => {
  45. return {
  46. id: `id${index}`, // id0,id1,id2... id属性名可自定义,locationById 方法中使用
  47. longitude: Math.random() / 2 + 116.15,
  48. latitude: Math.random() / 2 + 39.61,
  49. }
  50. })
  51. }
  52. function transformProj (data) {
  53. data.forEach(item => {
  54. const coords = CTMapOl.proj.fromLonLat([item.longitude, item.latitude])
  55. item.longitude = coords[0]
  56. item.latitude = coords[1]
  57. })
  58. return data
  59. }
  60. const { Style, Text, Circle, Fill, Stroke } = CTMapOl.style
  61. const markerStyle = new Style({
  62. image: new Circle({
  63. radius: 10,
  64. fill: new Fill({
  65. color: 'rgba(72,255,0,0.8)'
  66. })
  67. })
  68. })
  69. const markerSelectedStyle = new Style({
  70. image: new Circle({
  71. radius: 10,
  72. fill: new Fill({
  73. color: 'red'
  74. })
  75. }),
  76. zIndex: Infinity
  77. })
  78. new Vue({
  79. el: '#app',
  80. data () {
  81. this.visible = true
  82. return {
  83. cluster: false
  84. }
  85. },
  86. mounted () {
  87. this.map = new CTMapOl.extend.InitMap({
  88. domId: 'map',
  89. zoom: 9
  90. })
  91. this.init()
  92. },
  93. methods: {
  94. init () {
  95. const data = transformProj(makeRandomData(3000))
  96. // 不要在 data 中定义 markers 变量,无需响应式
  97. this.markers = new CTMapOl.extend.Markers(this.map.map, data, {
  98. // cluster: this.cluster, // 是否启用聚合,可动态切换
  99. // multiSelect: true, // 是否为多选模式,不可动态切换
  100. getCoordsFunc: item => [item.longitude, item.latitude],
  101. markerStyle: feature => {
  102. const selected = feature.get('selected')
  103. // 尽量在函数外预先生成固定不变部分,可提高性能
  104. return selected ? markerSelectedStyle : markerStyle
  105. },
  106. clusterStyleFunc: count => {
  107. return new Style({
  108. image: new Circle({
  109. radius: 10,
  110. fill: new Fill({
  111. color: 'blue'
  112. })
  113. }),
  114. text: new Text({
  115. text: count,
  116. fill: new Fill({
  117. color: '#fff'
  118. })
  119. })
  120. })
  121. },
  122. // onClick 多选模式下 feature 参数为 undefined,data 为
  123. // {
  124. // type, // select(选中), deSelect(取消选中)
  125. // feature, // 点击的标记
  126. // selectedFeatures // 点击后全部已选中标记
  127. // }
  128. onClick: (data, feature) => { console.log('选中:', data, feature) },
  129. onClickCluster: feature => { console.log('选中聚合点:', feature) },
  130. // onDeSelect 仅在单选模式,点击地图空白区域时触发,feature 为被取消选中的标记
  131. onDeSelect: (data, feature) => { console.log('取消选中:', data, feature) }
  132. })
  133. },
  134. toggleVisible() {
  135. this.visible = !this.visible
  136. this.markers.setVisible(this.visible)
  137. },
  138. toggleCluster () {
  139. this.cluster = !this.cluster
  140. this.markers.enableCluster(this.cluster)
  141. },
  142. selectMarker () {
  143. const markerFeature = this.markers.selectMarker(item => item.id === 'id9')
  144. console.log(markerFeature)
  145. },
  146. selectReverse () {
  147. const reverse = this.markers.selectReverse()
  148. console.log('选中:', reverse)
  149. },
  150. deSelect() {
  151. // 单选模式无需传参
  152. this.markers.deSelectMarker()
  153. // 多选模式需传查询方法
  154. // this.markers.deSelectMarker(item => item.id === 'id11')
  155. },
  156. clearSelected() {
  157. // 单选多选模式均可
  158. this.markers.clearSelected()
  159. },
  160. setData (replace) {
  161. const data = transformProj(makeRandomData(1000))
  162. this.markers.addMarkers(data, replace)
  163. }
  164. }
  165. })
  166. </script>
  167. </html>