index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <div
  3. v-loading="loading"
  4. element-loading-text="地图加载中..."
  5. class="map-box"
  6. :class="{ 'no-pointer': isDesign }"
  7. >
  8. <div
  9. :id="`map-${config.code}`"
  10. style="width: 100%; height: 100%;"
  11. />
  12. </div>
  13. </template>
  14. <script>
  15. import AMapLoader from '@amap/amap-jsapi-loader'
  16. import cloneDeep from 'lodash/cloneDeep'
  17. export default {
  18. name: 'RemoteMap',
  19. props: {
  20. config: {
  21. type: Object,
  22. default: () => ({})
  23. }
  24. },
  25. data () {
  26. return {
  27. map: null,
  28. loading: false
  29. }
  30. },
  31. computed: {
  32. option () {
  33. return this.config.option
  34. },
  35. optionData () {
  36. return this.option.data
  37. },
  38. customize () {
  39. return this.option.customize
  40. },
  41. isDesign () {
  42. return (window?.BS_CONFIG?.routers?.designUrl || '/big-screen/design') === this.$route.path
  43. }
  44. },
  45. mounted () {
  46. this.initMap(this.customize)
  47. },
  48. methods: {
  49. initMap (customize) {
  50. this.loading = true
  51. this.updateKey = 0
  52. AMapLoader.load({
  53. key: customize.mapKey || '1b0a1423b70bbcbc20c9c87327e5e94e', // 申请好的Web端开发者Key,首次调用 load 时必填
  54. version: '1.4.15', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  55. plugins: [
  56. 'AMap.ToolBar',
  57. 'AMap.Scale',
  58. 'AMap.HawkEye',
  59. 'AMap.MapType',
  60. 'AMap.Geolocation'
  61. ]
  62. }).then(() => {
  63. // 创建地图
  64. // eslint-disable-next-line no-undef
  65. this.map = new AMap.Map(`map-${this.config.code}`, {
  66. resizeEnable: true, // 是否监控地图容器尺寸变化
  67. lang: customize.lang,
  68. mapStyle: `amap://styles/${customize.mapStyle}`,
  69. center: [customize.lng, customize.lat],
  70. features: customize.features,
  71. zoom: customize.zoom,
  72. viewMode: customize.viewMode,
  73. plugins: ['AMap.ToolBar', 'AMap.Scale', 'AMap.MapType', 'AMap.Geolocation']
  74. })
  75. this.loading = false
  76. // eslint-disable-next-line no-undef
  77. this.map.addControl(new AMap.ToolBar())
  78. // 在图面添加比例尺控件,展示地图在当前层级和纬度下的比例尺
  79. // eslint-disable-next-line no-undef
  80. this.map.addControl(new AMap.Scale())
  81. // 在图面添加类别切换控件,实现默认图层与卫星图、实施交通图层之间切换的控制
  82. // eslint-disable-next-line no-undef
  83. this.map.addControl(new AMap.MapType())
  84. // 在图面添加定位控件,用来获取和展示用户主机所在的经纬度位置
  85. // eslint-disable-next-line no-undef
  86. this.map.addControl(new AMap.Geolocation())
  87. let marker = null // 用于存储标记对象的变量
  88. if (customize.markerSpan) {
  89. // 创建自定义标记内容
  90. const markerContent = document.createElement('div')
  91. markerContent.style.position = 'absolute'
  92. markerContent.style.width = '25px'
  93. markerContent.style.height = '34px'
  94. // 创建标记图标
  95. const markerImg = document.createElement('img')
  96. markerImg.src = '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-red.png'
  97. markerImg.style.width = '25px'
  98. markerImg.style.height = '34px'
  99. markerContent.appendChild(markerImg)
  100. // 创建标记文本
  101. const markerSpan = document.createElement('span')
  102. markerSpan.className = 'marker'
  103. markerSpan.innerHTML = customize.markerSpan
  104. markerContent.appendChild(markerSpan)
  105. // 删除之前的标记(如果存在)
  106. if (marker) {
  107. this.map.remove(marker)
  108. }
  109. // 创建自定义标记
  110. // eslint-disable-next-line no-undef
  111. marker = new AMap.Marker({
  112. position: [customize.markerLng, customize.markerLat],
  113. content: markerContent,
  114. // eslint-disable-next-line no-undef
  115. offset: new AMap.Pixel(0, 0) // 设置标记偏移,使其指向标记位置
  116. })
  117. // 将标记添加到地图中
  118. this.map.add(marker)
  119. // 动态修改标记的 right 位置
  120. const markerElement = marker.getContent()
  121. const markerTextElement = markerElement.querySelector('.marker')
  122. markerTextElement.style.right = 0 // 设置初始的 right 值
  123. if (markerTextElement) {
  124. setTimeout(() => {
  125. markerTextElement.style.right = `-${markerTextElement.clientWidth}px` // 设置新的 right 值
  126. }, 100)
  127. }
  128. }
  129. })
  130. },
  131. customStyle (config) {
  132. if (config && config.option && config.option.customize) {
  133. this.initMap(config.option.customize)
  134. }
  135. }
  136. }
  137. }
  138. </script>
  139. <style scoped></style>
  140. <style lang="scss" scoped>
  141. .no-pointer {
  142. pointer-events: none;
  143. }
  144. .map-box {
  145. width: 100%;
  146. height: 100%;
  147. z-index: 999;
  148. ::v-deep .amap-marker-content img {
  149. width: 25px;
  150. height: 34px;
  151. }
  152. ::v-deep .marker {
  153. position: absolute;
  154. top: -20px;
  155. right: -118px;
  156. color: #fff;
  157. padding: 4px 10px;
  158. box-shadow: 1px 1px 1px rgba(10, 10, 10, .2);
  159. white-space: nowrap;
  160. font-size: 12px;
  161. font-family: "";
  162. background-color: #25A5F7;
  163. border-radius: 3px;
  164. }
  165. }
  166. </style>