use-g-map-tsa.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import store from '/@/store'
  2. import { getRoot } from '/@/root'
  3. import { EDeviceTypeName } from '/@/types'
  4. import { getDeviceBySn } from '/@/api/manage'
  5. import { message } from 'ant-design-vue'
  6. import dockIcon from '/@/assets/icons/dock.png'
  7. import rcIcon from '/@/assets/icons/rc.png'
  8. import droneIcon from '/@/assets/icons/drone.png'
  9. import { getWorkspaceId } from '/@/utils/index'
  10. export function deviceTsaUpdate() {
  11. const root = getRoot()
  12. let AMap = root.$aMap
  13. const icons = new Map([
  14. [EDeviceTypeName.Aircraft, droneIcon],
  15. [EDeviceTypeName.Gateway, rcIcon],
  16. [EDeviceTypeName.Dock, dockIcon]
  17. ])
  18. const markers = store.state.markerInfo.coverMap
  19. const paths = store.state.markerInfo.pathMap
  20. let trackLine = null as any
  21. function getTrackLineInstance() {
  22. if (!trackLine) {
  23. trackLine = new AMap.Polyline({
  24. map: root.$map,
  25. strokeColor: '#939393' // 线颜色
  26. })
  27. }
  28. return trackLine
  29. }
  30. function initIcon(type: number) {
  31. return new AMap.Icon({
  32. image: icons.get(type),
  33. imageSize: new AMap.Size(40, 40),
  34. size: new AMap.Size(40, 40)
  35. })
  36. }
  37. function initMarker(type: number, name: string, sn: string, lng?: number, lat?: number) {
  38. if (markers[sn]) {
  39. return
  40. }
  41. if (root.$aMap === undefined) {
  42. return
  43. }
  44. AMap = root.$aMap
  45. if (type !== EDeviceTypeName.Aircraft) {// 加入判断屏蔽小飞机图标
  46. markers[sn] = new AMap.Marker({
  47. position: new AMap.LngLat(lng || 121.48, lat || 31.22),
  48. icon: initIcon(type),
  49. title: name,
  50. anchor: 'top-center',
  51. offset: [0, -20],
  52. })
  53. root.$map.add(markers[sn])
  54. }
  55. }
  56. const removeDeviceMarker = (sn: string) => {
  57. if (!markers[sn]) {
  58. return
  59. }
  60. root.$map.remove(markers[sn])
  61. delete markers[sn]
  62. }
  63. function removeMarker(sn: string) {
  64. if (!markers[sn]) {
  65. return
  66. }
  67. root.$map.remove(markers[sn])
  68. getTrackLineInstance().setPath([])
  69. delete markers[sn]
  70. delete paths[sn]
  71. }
  72. function addMarker(sn: string, lng?: number, lat?: number) {
  73. getDeviceBySn(getWorkspaceId(), sn)
  74. .then(data => {
  75. if (data.code !== 0) {
  76. message.error(data.message)
  77. return
  78. }
  79. initMarker(data.data.domain, data.data.nickname, sn, lng, lat)
  80. })
  81. }
  82. function moveTo(sn: string, lng: number, lat: number) {
  83. let marker = markers[sn]
  84. if (marker) {
  85. marker.moveTo([lng, lat], {
  86. duration: 1800,
  87. autoRotation: true
  88. })
  89. } else {
  90. addMarker(sn, lng, lat)
  91. marker = markers[sn]
  92. }
  93. }
  94. return {
  95. marker: markers,
  96. initMarker,
  97. removeDeviceMarker,
  98. removeMarker,
  99. moveTo
  100. }
  101. }