index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <template>
  2. <div class="main">
  3. <div class="tree">
  4. <el-input
  5. v-model="treeText"
  6. placeholder="Please enter keyword"
  7. @input="handleTreeFilter"
  8. />
  9. <el-tree-v2
  10. ref="ref_tree"
  11. :data="treeData"
  12. :props="{
  13. value: 'mapId',
  14. label: 'mapLabel',
  15. children: 'children'
  16. }"
  17. :height="800"
  18. :filter-method="filterMethod"
  19. >
  20. <template #default="{ node }">
  21. <div>
  22. <span>{{ node.label }}</span>
  23. <el-link v-if="!node.data._show" href="javascript:;" type="primary" @click.stop="onShow(node)">显示</el-link>
  24. <el-link v-else href="javascript:;" type="primary" @click.stop="onHide(node)">隐藏</el-link>
  25. </div>
  26. </template>
  27. </el-tree-v2>
  28. </div>
  29. <div class="map">
  30. <EasyMapGLComponent
  31. @easyMapGLLoad="mapGLLoad"/>
  32. </div>
  33. </div>
  34. </template>
  35. <script lang="ts">
  36. import {
  37. defineComponent,
  38. computed,
  39. onMounted,
  40. ref,
  41. reactive,
  42. watch,
  43. getCurrentInstance,
  44. ComponentInternalInstance,
  45. toRefs,
  46. nextTick, createApp
  47. } from 'vue'
  48. import {useStore} from 'vuex'
  49. import {useRouter, useRoute} from 'vue-router'
  50. import {ElMessage, ElMessageBox} from "element-plus";
  51. import xqysJson from './xqys.json'
  52. import * as format from "ol/format";
  53. import defaultImg from "./img/AIS.svg";
  54. import axBayonetListImg from "./img/tools-element-port-bayonet.svg";
  55. import axSiteListImg from "./img/tools-element-port-industry.svg";
  56. import governmentalListImg from "./img/tools-element-shoreline-government.svg";
  57. import islandListImg from "./img/tools-element-shoreline-island.svg";
  58. import monitoringListImg from "./img/tools-element-water-port.svg";
  59. import radarListImg from "./img/tools-element-police-PDT.svg";
  60. import terminalListImg from "./img/tools-element-shoreline-policing.svg";
  61. import waterBasedFacilitiesListImg from "./img/tools-element-port-facilities.svg";
  62. import deptInfoImg from "./img/tools-element-dept-0.svg";
  63. import ShipHover from "@/views/ship-test/business/v2/ship-hover.vue";
  64. import mapboxgl from "mapbox-gl";
  65. export default defineComponent({
  66. name: '',
  67. components: {},
  68. props: {},
  69. setup(props, {emit}) {
  70. const store = useStore();
  71. const router = useRouter();
  72. const route = useRoute();
  73. const that = (getCurrentInstance() as ComponentInternalInstance).appContext.config.globalProperties
  74. const state = reactive({
  75. map: <any>null,
  76. mapFunc: <any>null,
  77. treeData: <any>[],
  78. sourceElement: 'source_element',
  79. popupHover: <any>null,
  80. treeText: ''
  81. })
  82. const ref_tree = ref()
  83. const mapGLLoad = (map, func) => {
  84. state.map = map
  85. state.mapFunc = func
  86. initMap()
  87. initTree()
  88. }
  89. const initTree = () => {
  90. state.treeData = []
  91. const formatData = (key, pName, idKey = 'id', labelKey = 'name') => {
  92. const obj = {
  93. mapId: key,
  94. mapLabel: pName,
  95. children: [],
  96. _show: true
  97. }
  98. xqysJson.data[key].forEach(v => {
  99. v.mapId = key + '_' + v[idKey]
  100. v.mapLabel = v[labelKey]
  101. v._show = true
  102. obj.children.push(v)
  103. })
  104. state.treeData.push(obj)
  105. }
  106. formatData('axAreaList', '岸线-预警区')
  107. formatData('axBayonetList', '岸线-卡口')
  108. formatData('axCoastlineList', '岸线-海岸线')
  109. formatData('axSiteList', '岸线-行业场所')
  110. formatData('deptInfo', '部门详情', 'deptId', 'deptName')
  111. formatData('districtList', '警务区')
  112. formatData('governmentalList', '政府机构')
  113. formatData('islandList', '岛屿')
  114. formatData('monitoringList', '视频监控')
  115. formatData('portList', '港区列表')
  116. formatData('radarList', '雷达')
  117. formatData('seaList', '水域-海域')
  118. formatData('terminalList', '自助报备终端')
  119. formatData('waterAreaList', '水域-警务区')
  120. formatData('waterBasedFacilitiesList', '水域-水上设施')
  121. setSourceData()
  122. }
  123. const initMap = () => {
  124. state.mapFunc.addImg('defaultImg', defaultImg)
  125. state.mapFunc.addImg('axBayonetListImg', axBayonetListImg)
  126. state.mapFunc.addImg('axSiteListImg', axSiteListImg)
  127. state.mapFunc.addImg('governmentalListImg', governmentalListImg)
  128. state.mapFunc.addImg('islandListImg', islandListImg)
  129. state.mapFunc.addImg('monitoringListImg', monitoringListImg)
  130. state.mapFunc.addImg('radarListImg', radarListImg)
  131. state.mapFunc.addImg('terminalListImg', terminalListImg)
  132. state.mapFunc.addImg('waterBasedFacilitiesListImg', waterBasedFacilitiesListImg)
  133. state.mapFunc.addImg('deptInfoImg', deptInfoImg)
  134. state.map.addSource(state.sourceElement, {
  135. type: 'geojson',
  136. data: {
  137. type: 'FeatureCollection',
  138. features: []
  139. }
  140. })
  141. state.map.addLayer({
  142. id: 'layer_element-line',
  143. type: "line",
  144. source: state.sourceElement,
  145. layout: {
  146. },
  147. paint: {
  148. 'line-color': ['get', 'lineColor'],
  149. 'line-width': ['get', 'lineWidth'],
  150. // 'line-dasharray': ['get', 'lineDasharray']
  151. }
  152. })
  153. state.map.addLayer({
  154. id: 'layer_element-polygon',
  155. type: "fill",
  156. source: state.sourceElement,
  157. paint: {
  158. 'fill-color': ['get', 'fillColor']
  159. }
  160. })
  161. state.map.addLayer({
  162. id: 'layer_element-symbol',
  163. type: "symbol",
  164. source: state.sourceElement,
  165. layout: {
  166. 'icon-allow-overlap': true,
  167. 'icon-ignore-placement': true,
  168. 'icon-image': [
  169. 'match',
  170. ['get', 'iconImage'], // 根据属性值'type'来匹配
  171. 'axBayonetListImg', 'axBayonetListImg',
  172. 'axSiteListImg', 'axSiteListImg',
  173. 'governmentalListImg', 'governmentalListImg',
  174. 'islandListImg', 'islandListImg',
  175. 'monitoringListImg', 'monitoringListImg',
  176. 'radarListImg', 'radarListImg',
  177. 'terminalListImg', 'terminalListImg',
  178. 'waterBasedFacilitiesListImg', 'waterBasedFacilitiesListImg',
  179. 'deptInfoImg', 'deptInfoImg',
  180. '' // 默认样式
  181. ],
  182. "symbol-spacing": 1,
  183. },
  184. paint: {
  185. 'icon-color': 'red',
  186. 'icon-halo-color': '#000000',
  187. 'icon-halo-width': 4,
  188. }
  189. })
  190. state.map.on('click', 'layer_element-line', (e) => {
  191. const features = state.map.queryRenderedFeatures(e.point);
  192. if (!features.length) {
  193. return;
  194. }
  195. const properties: any = features[0].properties;
  196. const html = '线_' + properties.mapLabel
  197. if (state.popupHover) {
  198. state.popupHover.setLngLat(e.lngLat).setHTML(html)
  199. } else {
  200. state.popupHover = new mapboxgl.Popup({
  201. closeOnClick: false
  202. }).setLngLat(e.lngLat).setHTML(html).addTo(state.map);
  203. state.popupHover.on('close', () => {
  204. state.popupHover = null
  205. })
  206. }
  207. });
  208. state.map.on('click', 'layer_element-polygon', (e) => {
  209. const features = state.map.queryRenderedFeatures(e.point);
  210. if (!features.length) {
  211. return;
  212. }
  213. const properties: any = features[0].properties;
  214. const html = '面_' + properties.mapLabel
  215. if (state.popupHover) {
  216. state.popupHover.setLngLat(e.lngLat).setHTML(html)
  217. } else {
  218. state.popupHover = new mapboxgl.Popup({
  219. closeOnClick: false
  220. }).setLngLat(e.lngLat).setHTML(html).addTo(state.map);
  221. state.popupHover.on('close', () => {
  222. state.popupHover = null
  223. })
  224. }
  225. });
  226. state.map.on('click', 'layer_element-symbol', (e) => {
  227. const features = state.map.queryRenderedFeatures(e.point);
  228. console.log(features)
  229. if (!features.length) {
  230. return;
  231. }
  232. const properties: any = features[0].properties;
  233. const html = '点_' + properties.mapLabel
  234. if (state.popupHover) {
  235. state.popupHover.setLngLat(e.lngLat).setHTML(html)
  236. } else {
  237. state.popupHover = new mapboxgl.Popup({
  238. closeOnClick: false
  239. }).setLngLat(e.lngLat).setHTML(html).addTo(state.map);
  240. state.popupHover.on('close', () => {
  241. state.popupHover = null
  242. })
  243. }
  244. });
  245. }
  246. const globalLineDash = [
  247. [0, 0], //实线
  248. [15, 15], //长虚线
  249. [5, 5] //虚线
  250. ]
  251. const setSourceData = () => {
  252. const arr: any = []
  253. state.treeData.forEach(p => {
  254. console.log(p)
  255. if (p.mapId === 'deptInfo') {
  256. } else {
  257. p.children.forEach(v => {
  258. try {
  259. const geometry: any = new format.WKT().readFeature(v.location).getGeometry()
  260. const obj = {
  261. type: 'Feature',
  262. geometry: {
  263. type: geometry.getType(),
  264. coordinates: geometry.getCoordinates()
  265. },
  266. properties: v
  267. }
  268. let fillColor = 'rgba(20, 129, 241, 0.3)'
  269. let lineColor = '#2860F1'
  270. let lineWidth = 1
  271. let lineDasharray = globalLineDash[0]
  272. if (v.regionalColor) {
  273. fillColor = v.regionalColor
  274. }
  275. if (v.segmentColor) {
  276. lineColor = v.segmentColor
  277. }
  278. if (v.segmentWidth) {
  279. lineWidth = v.segmentWidth
  280. }
  281. if (v.segmentType) {
  282. lineDasharray = globalLineDash[Number(v.segmentType)]
  283. }
  284. obj.properties.fillColor = fillColor
  285. obj.properties.lineColor = lineColor
  286. obj.properties.lineWidth = lineWidth
  287. obj.properties.lineDasharray = lineDasharray
  288. switch (p.mapId) {
  289. case 'axBayonetList': obj.properties.iconImage = 'axBayonetListImg'
  290. break
  291. case 'axSiteList': obj.properties.iconImage = 'axSiteListImg'
  292. break
  293. case 'governmentalList': obj.properties.iconImage = 'governmentalListImg'
  294. break
  295. case 'islandList': obj.properties.iconImage = 'islandListImg'
  296. break
  297. case 'monitoringList': obj.properties.iconImage = 'monitoringListImg'
  298. break
  299. case 'radarList': obj.properties.iconImage = 'radarListImg'
  300. break
  301. case 'terminalList': obj.properties.iconImage = 'terminalListImg'
  302. break
  303. case 'waterBasedFacilitiesList': obj.properties.iconImage = 'waterBasedFacilitiesListImg'
  304. break
  305. }
  306. arr.push(obj)
  307. } catch (e) {
  308. }
  309. })
  310. }
  311. })
  312. state.map.getSource(state.sourceElement).setData({
  313. type: 'FeatureCollection',
  314. features: arr
  315. })
  316. }
  317. const filterMethod = (text: string, node: any) => {
  318. return node.mapLabel?.includes(text)
  319. }
  320. const handleTreeFilter = (text) => {
  321. ref_tree.value?.filter(text)
  322. }
  323. const onShow = (node) => {
  324. console.log(node)
  325. node.data._show = true
  326. node.children?.forEach(v => {
  327. v._show = true
  328. })
  329. setSourceData()
  330. }
  331. const onHide = (node) => {
  332. console.log(node)
  333. node.data._show = false
  334. node.children?.forEach(v => {
  335. v._show = false
  336. })
  337. setSourceData()
  338. }
  339. onMounted(() => {
  340. console.log(xqysJson)
  341. })
  342. return {
  343. ...toRefs(state),
  344. mapGLLoad,
  345. setSourceData,
  346. filterMethod,
  347. handleTreeFilter,
  348. ref_tree,
  349. onShow,
  350. onHide
  351. }
  352. },
  353. })
  354. </script>
  355. <style scoped lang="scss">
  356. .main {
  357. width: 100%;
  358. height: 100vh;
  359. display: flex;
  360. .tree {
  361. width: 400px;
  362. height: 100%;
  363. }
  364. .map {
  365. flex: 1;
  366. }
  367. }
  368. </style>