wangrong
2026-08-30 761e5c5fa871959f60e15c971c8d83f2b53d0af5
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
<script setup lang="ts">
import { nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
 
const domId = ref('de-map-container')
const mapInstance = ref(null)
const mapReloading = ref(false)
 
const props = defineProps<{
  mapKey: string
  securityCode?: string
}>()
 
const loadMap = () => {
  if (!props.mapKey) {
    return
  }
  const mykey = props.mapKey
  const url = `https://map.qq.com/api/gljs?v=1.exp&key=${mykey}`
 
  loadScript(url)
    .then(() => {
      if (mapInstance.value) {
        mapInstance.value?.destroy()
        mapInstance.value = null
        mapReloading.value = true
        setTimeout(() => {
          domId.value = domId.value + '-de-'
          mapReloading.value = false
          nextTick(() => {
            createMapInstance()
          })
        }, 1500)
 
        return
      }
      createMapInstance()
    })
    .catch(e => {
      console.error(e)
      if (mapInstance.value) {
        mapInstance.value.destroy()
        mapInstance.value = null
      }
    })
}
const createMapInstance = () => {
  if (window.TMap) {
    const center = new window.TMap.LatLng(39.90923, 116.397428)
    mapInstance.value = new window.TMap.Map(document.getElementById(domId.value), {
      viewMode: '2D',
      zoom: 11,
      center: center
    })
    mapInstance.value?.removeControl(window.TMap.constants.DEFAULT_CONTROL_ID.ZOOM)
    mapInstance.value?.removeControl(window.TMap.constants.DEFAULT_CONTROL_ID.ROTATION)
    mapInstance.value?.removeControl(window.TMap.constants.DEFAULT_CONTROL_ID.SCALE)
  }
}
const loadScript = (url: string) => {
  return new Promise(function (resolve, reject) {
    const scriptId = 'de-qq-script-id'
    let dom = document.getElementById(scriptId)
    if (dom) {
      dom.parentElement?.removeChild(dom)
      dom = null
      window.TMap = null
    }
    const script = document.createElement('script')
 
    script.id = scriptId
    script.onload = function () {
      return resolve(null)
    }
    script.onerror = function () {
      return reject(new Error('Load script from '.concat(url, ' failed')))
    }
    script.src = url
    const head = document.head || document.getElementsByTagName('head')[0]
    ;(document.body || head).appendChild(script)
  })
}
 
onMounted(() => {
  loadMap()
})
onBeforeUnmount(() => {
  const scriptId = 'de-qq-script-id'
  let dom = document.getElementById(scriptId)
  if (dom) {
    dom.parentElement?.removeChild(dom)
    dom = null
    window.TMap = null
  }
})
</script>
 
<template>
  <div class="de-map-container" v-if="!mapReloading" :id="domId" />
</template>
 
<style scoped lang="less">
.de-map-container {
  height: 100%;
  width: 100%;
  position: relative;
}
</style>