You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.5 KiB

3 years ago
import store from '/@/store'
import { getRoot } from '/@/root'
import { ELocalStorageKey } from '/@/types'
import { getDeviceBySn } from '/@/api/manage'
import { message } from 'ant-design-vue'
export function deviceTsaUpdate () {
const root = getRoot()
3 years ago
const AMap = root.$aMap
3 years ago
const icons: {
[key: string]: string
} = {
3 years ago
'sub-device': '/@/assets/icons/drone.png',
3 years ago
'gateway': '/@/assets/icons/rc.png',
'dock': '/@/assets/icons/dock.png'
}
const markers = store.state.markerInfo.coverMap
const paths = store.state.markerInfo.pathMap
3 years ago
// Fix: 航迹初始化报错
// TODO: 从时序上解决
let trackLine = null as any
function getTrackLineInstance () {
if (!trackLine) {
trackLine = new AMap.Polyline({
map: root.$map,
strokeColor: '#939393' // 线颜色
})
}
return trackLine
}
3 years ago
function initIcon (type: string) {
return new AMap.Icon({
image: icons[type],
imageSize: new AMap.Size(40, 40)
})
}
function initMarker (type: string, name: string, sn: string, lng?: number, lat?: number) {
if (markers[sn]) {
return
}
markers[sn] = new AMap.Marker({
3 years ago
position: new AMap.LngLat(lng || 113.935913, lat || 22.525335),
3 years ago
icon: initIcon(type),
title: name,
anchor: 'top-center',
offset: [0, -20],
})
3 years ago
root.$map.add(markers[sn])
3 years ago
// markers[sn].on('moving', function (e: any) {
// let path = paths[sn]
// if (!path) {
// paths[sn] = e.passedPath
// return
// }
// path.push(e.passedPath[0])
// path.push(e.passedPath[1])
3 years ago
// getTrackLineInstance().setPath(path)
3 years ago
// })
}
3 years ago
3 years ago
function removeMarker (sn: string) {
if (!markers[sn]) {
return
}
3 years ago
root.$map.remove(markers[sn])
getTrackLineInstance().setPath([])
3 years ago
delete markers[sn]
delete paths[sn]
}
3 years ago
function addMarker (sn: string, lng?: number, lat?: number) {
3 years ago
getDeviceBySn(localStorage.getItem(ELocalStorageKey.WorkspaceId)!, sn)
.then(data => {
if (data.code !== 0) {
message.error(data.message)
return
}
initMarker(data.data.domain, data.data.nickname, sn, lng, lat)
})
}
3 years ago
function moveTo (sn: string, lng: number, lat: number) {
3 years ago
let marker = markers[sn]
if (!marker) {
addMarker(sn, lng, lat)
marker = markers[sn]
return
}
marker.moveTo([lng, lat], {
duration: 1800,
autoRotation: true
})
}
3 years ago
3 years ago
return {
marker: markers,
initMarker,
removeMarker,
moveTo
}
}