index.vue 4.8 KB

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