|
|
@@ -23,6 +23,8 @@ const mapContainer = ref(null)
|
|
|
const mapError = ref('')
|
|
|
let mapInstance = null
|
|
|
let overlays = []
|
|
|
+let bmapLoadTimer = null
|
|
|
+let disposed = false
|
|
|
|
|
|
const convertedCenter = computed(() => convertCoord(props.center.lng, props.center.lat))
|
|
|
|
|
|
@@ -41,16 +43,25 @@ function waitForBMapGL() {
|
|
|
resolve(window.BMapGL)
|
|
|
return
|
|
|
}
|
|
|
+ if (disposed) {
|
|
|
+ reject(new Error('GIS map component disposed'))
|
|
|
+ return
|
|
|
+ }
|
|
|
let attempts = 0
|
|
|
- const timer = setInterval(() => {
|
|
|
+ bmapLoadTimer = setInterval(() => {
|
|
|
attempts += 1
|
|
|
+ if (disposed) {
|
|
|
+ clearInterval(bmapLoadTimer)
|
|
|
+ reject(new Error('GIS map component disposed'))
|
|
|
+ return
|
|
|
+ }
|
|
|
if (window.BMapGL) {
|
|
|
- clearInterval(timer)
|
|
|
+ clearInterval(bmapLoadTimer)
|
|
|
resolve(window.BMapGL)
|
|
|
return
|
|
|
}
|
|
|
if (attempts >= 100) {
|
|
|
- clearInterval(timer)
|
|
|
+ clearInterval(bmapLoadTimer)
|
|
|
reject(new Error('百度地图加载超时'))
|
|
|
}
|
|
|
}, 200)
|
|
|
@@ -61,6 +72,7 @@ async function initMap() {
|
|
|
if (!mapContainer.value) return
|
|
|
try {
|
|
|
const BMapGL = await waitForBMapGL()
|
|
|
+ if (disposed || !mapContainer.value) return
|
|
|
mapInstance = new BMapGL.Map(mapContainer.value, { enableMapClick: true })
|
|
|
const [lng, lat] = convertedCenter.value
|
|
|
mapInstance.centerAndZoom(new BMapGL.Point(lng, lat), props.zoom)
|
|
|
@@ -223,6 +235,11 @@ onMounted(async () => {
|
|
|
})
|
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
+ disposed = true
|
|
|
+ if (bmapLoadTimer) {
|
|
|
+ clearInterval(bmapLoadTimer)
|
|
|
+ bmapLoadTimer = null
|
|
|
+ }
|
|
|
clearScene()
|
|
|
try {
|
|
|
mapInstance?.destroy()
|