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