use-g-map-cover.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. import { EFlightAreaType } from '../types/flight-area'
  2. import pin19be6b from '/@/assets/icons/pin-19be6b.svg'
  3. import pin212121 from '/@/assets/icons/pin-212121.svg'
  4. import pin2d8cf0 from '/@/assets/icons/pin-2d8cf0.svg'
  5. import pinb620e0 from '/@/assets/icons/pin-b620e0.svg'
  6. import pine23c39 from '/@/assets/icons/pin-e23c39.svg'
  7. import pineffbb00 from '/@/assets/icons/pin-ffbb00.svg'
  8. import { getRoot } from '/@/root'
  9. import rootStore from '/@/store'
  10. import { GeojsonCoordinate } from '/@/types/map'
  11. export function useGMapCover() {
  12. const root = getRoot()
  13. const AMap = root.$aMap
  14. const normalColor = '#2D8CF0'
  15. const store = rootStore
  16. const coverMap = store.state.coverMap
  17. const flightAreaColorMap = {
  18. [EFlightAreaType.DFENCE]: '#19be6b',
  19. [EFlightAreaType.NFZ]: '#ff0000',
  20. }
  21. const disableColor = '#b3b3b3'
  22. function AddCoverToMap(cover: any) {
  23. root.$map.add(cover)
  24. coverMap[cover.getExtData().id] = [cover]
  25. }
  26. function getPinIcon(color?: string) {
  27. const colorObj: {
  28. [key: number | string]: any
  29. } = {
  30. '2d8cf0': pin2d8cf0,
  31. '19be6b': pin19be6b,
  32. 212121: pin212121,
  33. b620e0: pinb620e0,
  34. e23c39: pine23c39,
  35. ffbb00: pineffbb00,
  36. }
  37. const iconName = (color?.replaceAll('#', '') || '').toLocaleLowerCase()
  38. return new AMap.Icon({
  39. image: colorObj[iconName],
  40. })
  41. }
  42. function init2DPin(name: string, coordinates: GeojsonCoordinate, color?: string, data?: {}) {
  43. const marker = new AMap.Marker({
  44. position: new AMap.LngLat(coordinates[0], coordinates[1]),
  45. title: name,
  46. icon: getPinIcon(color),
  47. extData: data
  48. })
  49. const text = new AMap.Text({
  50. position: new AMap.LngLat(coordinates[0], coordinates[1]),
  51. offset: new AMap.Pixel(-30, -30),
  52. text: name,
  53. style: {
  54. fontSize: 12,
  55. padding: 0,
  56. backgroundColor: 'transparent',
  57. borderColor: 'transparent',
  58. },
  59. extData: {
  60. ...data,
  61. id: data.id + '_text',
  62. },
  63. })
  64. AddCoverToMap(text)
  65. marker.on('click', function (e: any) {
  66. const options = marker.getOptions()
  67. const id = options.extData.id;
  68. store.commit('SET_MAP_CLICK_ELEMENT', {
  69. id: id,
  70. type: 'DEFAULT',
  71. });
  72. })
  73. AddCoverToMap(marker)
  74. }
  75. function initPolyline(name: string, coordinates: GeojsonCoordinate[], color?: string, data?: {}) {
  76. const path = [] as GeojsonCoordinate[]
  77. coordinates.forEach(coordinate => {
  78. path.push(new AMap.LngLat(coordinate[0], coordinate[1]))
  79. })
  80. const polyline = new AMap.Polyline({
  81. path: path,
  82. strokeColor: color || normalColor,
  83. strokeOpacity: 1,
  84. strokeWeight: 2,
  85. strokeStyle: 'solid',
  86. extData: data
  87. })
  88. const coordinatesList = coordinates[0];
  89. const text = new AMap.Text({
  90. position: new AMap.LngLat(coordinatesList[0], coordinatesList[1]),
  91. offset: new AMap.Pixel(-30, -30),
  92. text: name,
  93. style: {
  94. fontSize: 12,
  95. padding: 0,
  96. backgroundColor: 'transparent',
  97. borderColor: 'transparent',
  98. },
  99. extData: {
  100. ...data,
  101. id: data.id + '_text',
  102. },
  103. })
  104. AddCoverToMap(text)
  105. polyline.on('click', function () {
  106. const options = polyline.getOptions()
  107. const id = options.extData.id;
  108. store.commit('SET_MAP_CLICK_ELEMENT', {
  109. id: id,
  110. type: 'DEFAULT',
  111. });
  112. })
  113. AddOverlayGroup(polyline)
  114. }
  115. function initPolygon(name: string, coordinates: GeojsonCoordinate[][], color?: string, data?: {}) {
  116. const path = [] as GeojsonCoordinate[]
  117. coordinates[0].forEach(coordinate => {
  118. path.push(new AMap.LngLat(coordinate[0], coordinate[1]))
  119. })
  120. const polygon = new AMap.Polygon({
  121. path: path,
  122. strokeOpacity: 1,
  123. strokeWeight: 2,
  124. fillColor: color || normalColor,
  125. fillOpacity: 0.4,
  126. strokeColor: color || normalColor,
  127. extData: data
  128. })
  129. const coordinatesList = coordinates[0][0];
  130. const text = new AMap.Text({
  131. position: new AMap.LngLat(coordinatesList[0], coordinatesList[1]),
  132. offset: new AMap.Pixel(-30, -30),
  133. text: name,
  134. style: {
  135. fontSize: 12,
  136. padding: 0,
  137. backgroundColor: 'transparent',
  138. borderColor: 'transparent',
  139. },
  140. extData: {
  141. ...data,
  142. id: data.id + '_text',
  143. },
  144. })
  145. AddCoverToMap(text)
  146. polygon.on('click', function () {
  147. const options = polygon.getOptions()
  148. const id = options.extData.id;
  149. store.commit('SET_MAP_CLICK_ELEMENT', {
  150. id: id,
  151. type: 'DEFAULT',
  152. });
  153. })
  154. AddOverlayGroup(polygon)
  155. }
  156. function AddOverlayGroup(overlayGroup: any) {
  157. root.$map.add(overlayGroup)
  158. const id = overlayGroup.getExtData().id
  159. coverMap[id] = [...(coverMap[id] || []), overlayGroup]
  160. }
  161. function removeCoverFromMap(id: string) {
  162. coverMap[id].forEach(cover => root.$map.remove(cover))
  163. coverMap[id] = []
  164. }
  165. function getElementFromMap(id: string): any[] {
  166. return coverMap[id]
  167. }
  168. function updatePhotoElement(id: string, name: string, url: string, coordinates: [number, number]) {
  169. const icon = new AMap.Icon({
  170. size: new AMap.Size(30, 30),
  171. image: url,
  172. imageSize: new AMap.Size(30, 30)
  173. })
  174. const marker = new AMap.Marker({
  175. position: new AMap.LngLat(coordinates[0], coordinates[1]),
  176. icon: icon,
  177. title: name,
  178. extData: {
  179. id: id
  180. }
  181. })
  182. marker.on('click', function (e: any) {
  183. const options = marker.getOptions()
  184. const id = options.extData.id;
  185. store.commit('SET_MAP_CLICK_ELEMENT', {
  186. id: id,
  187. type: 'PHOTO',
  188. });
  189. })
  190. AddCoverToMap(marker)
  191. }
  192. function updatePinElement(id: string, name: string, coordinates: GeojsonCoordinate, color?: string) {
  193. init2DPin(name, coordinates, color, {
  194. id: id,
  195. name: name
  196. })
  197. }
  198. function updatePolylineElement(id: string, name: string, coordinates: GeojsonCoordinate[], color?: string) {
  199. initPolyline(name, coordinates, color, {
  200. id: id,
  201. name: name
  202. })
  203. }
  204. function updatePolygonElement(id: string, name: string, coordinates: GeojsonCoordinate[][], color?: string) {
  205. initPolygon(name, coordinates, color, {
  206. id: id,
  207. name: name
  208. })
  209. }
  210. function initTextInfo(content: string, coordinates: GeojsonCoordinate, id: string) {
  211. const info = new AMap.Text({
  212. text: content,
  213. position: new AMap.LngLat(coordinates[0], coordinates[1]),
  214. extData: { id: id, type: 'text' },
  215. anchor: 'top-center',
  216. style: {
  217. background: 'none',
  218. borderStyle: 'none',
  219. fontSize: '16px',
  220. },
  221. })
  222. AddOverlayGroup(info)
  223. }
  224. function initFlightAreaCircle(name: string, radius: number, position: GeojsonCoordinate, data: { id: string, type: EFlightAreaType, enable: boolean }) {
  225. const circle = new AMap.Circle({
  226. strokeColor: data.enable ? flightAreaColorMap[data.type] : disableColor,
  227. strokeOpacity: 1,
  228. strokeWeight: 6,
  229. extData: data,
  230. strokeStyle: 'dashed',
  231. strokeDasharray: EFlightAreaType.NFZ === data.type ? [10, 2] : [10, 1, 2],
  232. fillColor: flightAreaColorMap[data.type],
  233. fillOpacity: EFlightAreaType.NFZ === data.type && data.enable ? 0.3 : 0,
  234. radius: radius,
  235. center: new AMap.LngLat(position[0], position[1]),
  236. })
  237. AddOverlayGroup(circle)
  238. initTextInfo(name, position, data.id)
  239. }
  240. function updateFlightAreaCircle(id: string, name: string, radius: number, position: GeojsonCoordinate, enable: boolean, type: EFlightAreaType) {
  241. const elements = getElementFromMap(id)
  242. if (elements && elements.length > 0) {
  243. let textIndex = elements.findIndex(ele => ele.getExtData()?.type === 'text')
  244. if (textIndex === -1) {
  245. textIndex = 1
  246. initTextInfo(name, position, id)
  247. } else {
  248. const text = elements[textIndex]
  249. text.setText(name)
  250. text.setPosition(position)
  251. }
  252. const element = elements[textIndex ^ 1]
  253. const options = element.getOptions()
  254. options.fillOpacity = EFlightAreaType.NFZ === type && enable ? 0.3 : 0
  255. options.strokeColor = enable ? flightAreaColorMap[type] : disableColor
  256. options.radius = radius
  257. options.center = new AMap.LngLat(position[0], position[1])
  258. element.setOptions(options)
  259. } else {
  260. initFlightAreaCircle(name, radius, position, { id, type, enable })
  261. }
  262. }
  263. function calcPolygonPosition(coordinate: GeojsonCoordinate[]): GeojsonCoordinate {
  264. const index = coordinate.length - 1
  265. return [(coordinate[0][0] + coordinate[index][0]) / 2.0, (coordinate[0][1] + coordinate[index][1]) / 2]
  266. }
  267. function initFlightAreaPolygon(name: string, coordinates: GeojsonCoordinate[], data: { id: string, type: EFlightAreaType, enable: boolean }) {
  268. const path = [] as GeojsonCoordinate[]
  269. coordinates.forEach(coordinate => {
  270. path.push(new AMap.LngLat(coordinate[0], coordinate[1]))
  271. })
  272. const polygon = new AMap.Polygon({
  273. path: path,
  274. strokeColor: data.enable ? flightAreaColorMap[data.type] : disableColor,
  275. strokeOpacity: 1,
  276. strokeWeight: 4,
  277. draggable: true,
  278. extData: data,
  279. strokeStyle: 'dashed',
  280. strokeDasharray: EFlightAreaType.NFZ === data.type ? [10, 2] : [10, 1, 2],
  281. fillColor: flightAreaColorMap[data.type],
  282. fillOpacity: EFlightAreaType.NFZ === data.type && data.enable ? 0.3 : 0,
  283. })
  284. AddOverlayGroup(polygon)
  285. initTextInfo(name, calcPolygonPosition(coordinates), data.id)
  286. }
  287. function updateFlightAreaPolygon(id: string, name: string, coordinates: GeojsonCoordinate[], enable: boolean, type: EFlightAreaType) {
  288. const elements = getElementFromMap(id)
  289. if (elements && elements.length > 0) {
  290. let textIndex = elements.findIndex(ele => ele.getExtData()?.type === 'text')
  291. if (textIndex === -1) {
  292. textIndex = 1
  293. initTextInfo(name, calcPolygonPosition(coordinates), id)
  294. } else {
  295. const text = elements[textIndex]
  296. text.setText(name)
  297. text.setPosition(calcPolygonPosition(coordinates))
  298. }
  299. const element = elements[textIndex ^ 1]
  300. const options = element.getOptions()
  301. const path = [] as GeojsonCoordinate[]
  302. coordinates.forEach(coordinate => {
  303. path.push(new AMap.LngLat(coordinate[0], coordinate[1]))
  304. })
  305. options.path = path
  306. options.fillOpacity = EFlightAreaType.NFZ === type && enable ? 0.3 : 0
  307. options.strokeColor = enable ? flightAreaColorMap[type] : disableColor
  308. element.setOptions(options)
  309. } else {
  310. initFlightAreaPolygon(name, coordinates, { id, type, enable })
  311. }
  312. }
  313. return {
  314. init2DPin,
  315. initPolyline,
  316. initPolygon,
  317. removeCoverFromMap,
  318. getElementFromMap,
  319. updatePhotoElement,
  320. updatePinElement,
  321. updatePolylineElement,
  322. updatePolygonElement,
  323. initFlightAreaCircle,
  324. initFlightAreaPolygon,
  325. updateFlightAreaPolygon,
  326. updateFlightAreaCircle,
  327. calcPolygonPosition,
  328. }
  329. }