food/src/views/layout/components/main/home/index.vue

98 lines
2.5 KiB
Vue
Raw Normal View History

2023-09-20 21:33:11 +08:00
<template>
<div class="h-100">
<map-box ref="mapbox" @map-ready="onMapReady">
<template #content>
<div v-show="mapReady">
<!-- <Statistics :plane="plane" /> -->
</div>
</template>
</map-box>
2023-09-20 21:33:11 +08:00
</div>
</template>
<script>
import MapBox from '@/components/MapBox'
import { waitForMapCanvasReady } from '@/utils'
// import Statistics from '@/components/Statistics'
2023-09-20 21:33:11 +08:00
export default {
name: 'Home',
data () {
return {
mapReady: false// 地图加载完成后 回调时 设置此值 让地图组件插槽内容 滞后显示
2023-09-20 21:33:11 +08:00
}
},
components: {
MapBox
// Statistics
2023-09-20 21:33:11 +08:00
},
computed: {
airList () {
return this.$store.state.airList
},
/**
* @description: 侧边栏显隐
*/
isCollapse () {
return this.$store.state.app.isCollapse
}
},
methods: {
// 地图组件回调地图加载完成后 执行
onMapReady () {
this.mapReady = true // 标记地图加载完成
this.makePlanes(this.airList)
},
2023-09-20 21:33:11 +08:00
/**
* @description: 创建飞机图标
*/
makePlanes (planes) {
this.$refs.mapbox.removePlanes()// 先清除画布上现有的飞机
planes.forEach((plane, index) => { // 创建所有飞机
let planeDefaultLonLat
2023-09-20 21:33:11 +08:00
if (localStorage.getItem(plane.name) !== null) { // 从本地缓存 拿飞机得初始位置
planeDefaultLonLat = JSON.parse(localStorage.getItem(plane.name))
plane.lon = planeDefaultLonLat.lon
plane.lat = planeDefaultLonLat.lat
2023-09-20 21:33:11 +08:00
} else {
// 缺省 给经纬度 小数点后随机第五位 即有个10米左右的随机距离
plane.lon = 100 + Number((Math.random() * 0.01).toFixed(5))
plane.lat = 40 + Number((Math.random() * 0.01).toFixed(5))
2023-09-20 21:33:11 +08:00
}
this.$refs.mapbox.makePlane(plane, index)
})
}
},
mounted () {
},
watch: {
/**
* @description: 飞机列表更新时候 更新地图
*/
2023-09-20 21:33:11 +08:00
airList: {
async handler () {
try {
// 等待地图画布准备好
await waitForMapCanvasReady(this.map)
// 画布准备好后执行你自己的逻辑
this.onMapReady()
} catch (err) {
console.debug('等待地图画布准备超时', err)
}
},
immediate: true
2023-09-20 21:33:11 +08:00
},
/**
* @description: 侧边栏显隐
2023-09-20 21:33:11 +08:00
*/
isCollapse () {
return this.$store.state.app.isCollapse
2023-09-20 21:33:11 +08:00
}
},
destroyed () {
}
}
</script>
<style></style>