index.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. <template>
  2. <div class="init-speed-track">
  3. <EasyMapComponent
  4. class="map"
  5. :showBaseSwitch="true"
  6. @easyMapLoad="mapLoad"
  7. />
  8. <div class="track">
  9. <el-card shadow="always">
  10. <template #header>
  11. <div class="card-header">
  12. <span>轨迹列表</span>
  13. <el-button-group>
  14. <template v-for="[key, value] in SourceMap">
  15. <el-button :color="value.color" size="small" @click="drawTrack(key)" style="color: white">{{value.label}}</el-button>
  16. </template>
  17. </el-button-group>
  18. </div>
  19. </template>
  20. <div class="track-line">
  21. <template v-for="(item, index) in trackList">
  22. <div class="line">
  23. <div class="label" :style="`color: ${SourceMap.get(item.type).color};`">{{SourceMap.get(item.type).label}}</div>
  24. <el-tooltip :enterable="false" placement="top" content="隐藏" v-if="item.show">
  25. <img class="__hover" src="./ship-track-visible.svg" @click="handleShow(false, item)"/>
  26. </el-tooltip>
  27. <el-tooltip :enterable="false" placement="top" content="显示" v-else>
  28. <img class="__hover" src="./ship-track-invisible.svg" @click="handleShow(true, item)"/>
  29. </el-tooltip>
  30. </div>
  31. </template>
  32. </div>
  33. </el-card>
  34. <el-card shadow="always">
  35. <template #header>
  36. <div class="card-header">
  37. <span>轨迹点列表</span>
  38. <el-button v-if="trackPointList.length > 0" type="primary" size="small" @click="onSubmit" style="color: white">保存</el-button>
  39. </div>
  40. </template>
  41. <div class="track-point">
  42. <template v-for="(item, index) in trackPointList">
  43. <div class="point">
  44. <div class="position">
  45. <span>{{item.position[0]}}</span><br/>
  46. <span>{{item.position[1]}}</span>
  47. </div>
  48. <div class="speed">
  49. <el-input-number v-model="item.speed" :precision="2" :step="0.1" :max="100" :min="0" @focus="onPointFocus(trackPointList[index - 1], item, trackPointList[index + 1])"/>
  50. </div>
  51. </div>
  52. </template>
  53. </div>
  54. </el-card>
  55. </div>
  56. </div>
  57. </template>
  58. <script lang="ts">
  59. import {
  60. defineComponent,
  61. ref,
  62. nextTick,
  63. onMounted,
  64. watch,
  65. computed,
  66. ComponentInternalInstance,
  67. reactive,
  68. toRefs,
  69. getCurrentInstance
  70. } from 'vue'
  71. import {useStore} from 'vuex'
  72. import * as source from "ol/source";
  73. import * as layer from "ol/layer";
  74. import * as style from "ol/style";
  75. import * as ol from "ol";
  76. import * as sphere from "ol/sphere";
  77. import * as interaction from "ol/interaction";
  78. import {createBox} from "ol/interaction/Draw";
  79. import {unByKey} from "ol/Observable";
  80. import { Geometry } from 'ol/geom';
  81. import { EventsKey } from 'ol/events';
  82. import { Coordinate } from 'ol/coordinate';
  83. import TrackStyle from './track-style'
  84. import axios from "axios";
  85. export default defineComponent({
  86. name: 'App',
  87. components: {},
  88. setup() {
  89. const store = useStore()
  90. const that = (getCurrentInstance() as ComponentInternalInstance).appContext.config.globalProperties
  91. const SourceMap = new Map(window.cusConfig.trackSource)
  92. const state = reactive({
  93. map: <any>null,
  94. mapFunc: null,
  95. trackPointList: [],
  96. trackList: <any>[],
  97. formTrackPointStartCount: 0,
  98. formTrackPointEndCount: 0,
  99. formTrackPointList: [],
  100. initTrackPointStartCount: 0,
  101. initTrackPointEndCount: 0,
  102. initTrackPointList: [],
  103. });
  104. const mapLoad = (map: null, func: null) => {
  105. state.map = map
  106. state.mapFunc = func
  107. }
  108. const startDraw = (cb: { (evt: any): void; (arg0: { feature: { getGeometry: () => any; }; }): void; }) => {
  109. let measureTooltipElement: HTMLDivElement | null;
  110. let helpTooltipElement: HTMLDivElement | null;
  111. const realLayer = state.map.getLayers().getArray().filter((v: { get: (arg0: string) => string; }) => v.get('layerName') === 'measureLayer')
  112. let sketch: { getGeometry: () => { (): any; new(): any; on: { (arg0: string, arg1: (evt: any) => void): any; new(): any; }; }; } | null;
  113. let helpTooltip: ol.Overlay;
  114. let measureTooltip: ol.Overlay;
  115. let continueMsg = '双击结束标绘';
  116. const createMeasureTooltip = () => {
  117. const id = 'measureTooltipElementId'
  118. if (measureTooltipElement) {
  119. state.map.removeOverlay(state.map.getOverlayById(id))
  120. measureTooltipElement.parentNode?.removeChild(measureTooltipElement);
  121. }
  122. measureTooltipElement = document.createElement('div');
  123. measureTooltipElement.className = 'tooltip tooltip-measure';
  124. measureTooltip = new ol.Overlay({
  125. id,
  126. element: measureTooltipElement,
  127. offset: [0, -15],
  128. positioning: 'bottom-center'
  129. });
  130. state.map.addOverlay(measureTooltip);
  131. }
  132. const createHelpTooltip = () => {
  133. const id = 'helpTooltipElementId'
  134. if (helpTooltipElement) {
  135. state.map.removeOverlay(state.map.getOverlayById(id))
  136. helpTooltipElement.parentNode?.removeChild(helpTooltipElement);
  137. }
  138. helpTooltipElement = document.createElement('div');
  139. helpTooltipElement.className = 'tooltip hidden';
  140. helpTooltip = new ol.Overlay({
  141. id,
  142. element: helpTooltipElement,
  143. offset: [15, 0],
  144. positioning: 'center-left'
  145. });
  146. state.map.addOverlay(helpTooltip);
  147. }
  148. const formatLength = (line: Geometry) => {
  149. // 获取投影坐标系
  150. const sourceProj = state.map.getView().getProjection();
  151. // ol/sphere里有getLength()和getArea()用来测量距离和区域面积,默认的投影坐标系是EPSG:3857, 其中有个options的参数,可以设置投影坐标系
  152. const length = sphere.getLength(line, {projection: sourceProj});
  153. // const length = getLength(line);
  154. let output;
  155. if (length > 100) {
  156. const km = Math.round((length / 1000) * 100) / 100;
  157. output = `${km} 千米 <br>${parseFloat(String(km * 0.53995)).toFixed(2)} 海里`;
  158. } else {
  159. output = `${Math.round(length * 100) / 100} m`;
  160. }
  161. return output;
  162. };
  163. const addInteraction = () => {
  164. const id = 'drawName'
  165. const draw = new interaction.Draw({
  166. type: 'LineString', //几何图形类型
  167. style: new style.Style({
  168. fill: new style.Fill({
  169. color: "rgba(255, 255, 255, 0.2)",
  170. }),
  171. stroke: new style.Stroke({
  172. color: "#f3584a",
  173. width: 2,
  174. }),
  175. image: new style.Circle({
  176. radius: 5,
  177. stroke: new style.Stroke({
  178. color: "rgba(0, 0, 0, 0.7)",
  179. }),
  180. fill: new style.Fill({
  181. color: "rgba(255, 255, 255, 0.2)",
  182. }),
  183. }),
  184. }),
  185. });
  186. draw.set(id, id)
  187. createMeasureTooltip(); //创建测量工具提示框
  188. createHelpTooltip(); //创建帮助提示框
  189. state.map.addInteraction(draw);
  190. let listener: EventsKey | EventsKey[];
  191. //绑定交互绘制工具开始绘制的事件
  192. const drawstartHandle = (evt: { feature: { getGeometry: () => { (): any; new(): any; on: { (arg0: string, arg1: (evt: any) => void): any; new(): any; }; }; } | null; coordinate: any; }) => {
  193. sketch = evt.feature; //绘制的要素
  194. let tooltipCoord = evt.coordinate;// 绘制的坐标
  195. //绑定change事件,根据绘制几何类型得到测量长度值或面积值,并将其设置到测量工具提示框中显示
  196. listener = sketch?.getGeometry().on('change', function (evt) {
  197. const geom = evt.target
  198. let output;
  199. output = formatLength(geom);//长度值
  200. tooltipCoord = geom.getLastCoordinate();//坐标
  201. if (measureTooltipElement) measureTooltipElement.innerHTML = output;//将测量值设置到测量工具提示框中显示
  202. measureTooltip.setPosition(tooltipCoord);//设置测量工具提示框的显示位置
  203. });
  204. }
  205. draw.on('drawstart', drawstartHandle);
  206. //绑定交互绘制工具结束绘制的事件
  207. const copy = (value: string) => {
  208. const str = document.createElement('input')
  209. str.setAttribute('value', value)
  210. document.body.appendChild(str)
  211. str.select()
  212. document.execCommand('copy')
  213. document.body.removeChild(str)
  214. }
  215. const drawendHandle = (evt: { feature: { getGeometry: () => any; }; }) => {
  216. state.map.removeInteraction(state.map.getInteractions().getArray().filter((v: { get: (arg0: string) => string; }) => v.get(id) === id)[0]);
  217. sketch = null; //置空当前绘制的要素对象
  218. measureTooltipElement?.parentNode?.removeChild(measureTooltipElement);
  219. measureTooltipElement = null; //置空测量工具提示框对象
  220. helpTooltipElement?.parentNode?.removeChild(helpTooltipElement);
  221. helpTooltipElement = null; //置空测量工具提示框对象
  222. unByKey(listener);
  223. draw.un('drawstart', drawstartHandle);
  224. draw.un('drawend', drawendHandle);
  225. state.map.removeInteraction(state.map.getInteractions().getArray().filter((v: { get: (arg0: string) => string; }) => v.get(id) === id)[0]);
  226. state.map.un('pointermove', pointerMoveHandler)
  227. cb(evt)
  228. }
  229. draw.on('drawend', drawendHandle);
  230. }
  231. addInteraction(); //调用加载绘制交互控件方法,添加绘图进行测量
  232. const pointerMoveHandler = (evt: { dragging: any; coordinate: Coordinate | undefined; }) => {
  233. if (evt.dragging) {
  234. return;
  235. }
  236. let helpMsg = '单击开始标绘';//当前默认提示信息
  237. //判断绘制几何类型设置相应的帮助提示信息
  238. if (sketch) {
  239. const geom = sketch.getGeometry()
  240. helpMsg = continueMsg;
  241. // if (geom.getType() === 'Polygon') {
  242. // helpMsg = continueMsg; //绘制多边形时提示相应内容
  243. // } else if (geom.getType() === 'LineString') {
  244. // helpMsg = continueMsg; //绘制线时提示相应内容
  245. // }
  246. }
  247. if (helpTooltipElement)helpTooltipElement.innerHTML = helpMsg; //将提示信息设置到对话框中显示
  248. helpTooltip.setPosition(evt.coordinate);//设置帮助提示框的位置
  249. helpTooltipElement?.classList.remove('hidden');//移除帮助提示框的隐藏样式进行显示
  250. };
  251. state.map.on('pointermove', pointerMoveHandler); //地图容器绑定鼠标移动事件,动态显示帮助提示框内容
  252. //地图绑定鼠标移出事件,鼠标移出时为帮助提示框设置隐藏样式
  253. // state.map.getViewport().on('mouseout', () => {
  254. // helpTooltipElement?.addClass('hidden');
  255. // });
  256. }
  257. const drawTrack = (trackSource: string) => {
  258. startDraw((evt) => {
  259. const geom = evt.feature.getGeometry()
  260. const pMap = new Map()
  261. state.trackPointList = geom.getCoordinates().map((v: any) => {
  262. const obj = {
  263. source: trackSource,
  264. position: v,
  265. speed: 0
  266. }
  267. pMap.set(`${v[0]}-${v[1]}`, obj)
  268. return obj
  269. })
  270. that.$easyMap.initShape({
  271. map: state.map,
  272. layerName: "form-track-point-line",
  273. layerZIndex: 9,
  274. list: [
  275. {
  276. easyMapParams: {
  277. id: new Date().getTime(),
  278. position: that.$easyMap.formatPosition.wptTwl(state.trackPointList.map(v => that.$easyMap.formatPosition.cptTwpt(v.position))),
  279. normalStyle: (f: any, r: any) => TrackStyle.trackLineStyle(f, r, state.map, SourceMap.get(trackSource)?.color, pMap, (s, p) => {
  280. state.formTrackPointStartCount++
  281. setTimeout(() => {
  282. state.formTrackPointEndCount++
  283. state.formTrackPointList.push(...p)
  284. if (state.formTrackPointStartCount === state.formTrackPointEndCount) {
  285. that.$easyMap.initShape({
  286. map: state.map,
  287. layerName: 'form-track-point',
  288. layerZIndex: 10,
  289. list: state.formTrackPointList.map((v, i) => {
  290. return {
  291. easyMapParams: {
  292. id: `form-track-point-${i}`,
  293. position: that.$easyMap.formatPosition.cptTwpt(v.position),
  294. normalStyle: TrackStyle.trackPointStyle(SourceMap.get(v.source).color, v.speed)
  295. }
  296. }
  297. })
  298. })
  299. state.formTrackPointStartCount = 0
  300. state.formTrackPointEndCount = 0
  301. state.formTrackPointList = []
  302. }
  303. }, 10)
  304. return s
  305. }),
  306. }
  307. }
  308. ]
  309. });
  310. })
  311. }
  312. const onPointFocus = (p1: any, p2: any, p3: any) => {
  313. that.$easyMap.getShapeView(state.map, [p1?.position, p2.position, p3?.position].filter(v => v))
  314. const radius = 25
  315. const longRadius = radius * Math.SQRT2
  316. that.$easyMap.initShape({
  317. map: state.map,
  318. layerName: "focus",
  319. layerZIndex: 20,
  320. list: [
  321. {
  322. easyMapParams: {
  323. id: 'focus',
  324. position: that.$easyMap.formatPosition.cptTwpt(p2.position),
  325. normalStyle: [new style.Style({ //图层样式
  326. image: new style.RegularShape({
  327. stroke: new style.Stroke({
  328. color: '#9F2EFF',
  329. width: 2,
  330. lineDash: [
  331. (longRadius * 3) / 10,
  332. (longRadius * 4) / 10,
  333. (longRadius * 3) / 10,
  334. 0
  335. ]
  336. }),
  337. radius1: radius,
  338. rotation: Math.PI / (180 / 45),
  339. points: 4
  340. })
  341. })]
  342. }
  343. }
  344. ]
  345. });
  346. }
  347. const onSubmit = () => {
  348. const obj = {
  349. type: state.trackPointList[0].source,
  350. lines: state.trackPointList.map(v => {
  351. return {
  352. lon: v.position[0],
  353. lat: v.position[1],
  354. speed: v.speed
  355. }
  356. })
  357. }
  358. const result = JSON.parse(JSON.stringify(obj))
  359. state.trackList.push(Object.assign(result, {show: false, ID: new Date().getTime()}))
  360. state.trackPointList = []
  361. that.$easyMap.initShape({
  362. map: state.map,
  363. layerName: "form-track-point-line",
  364. layerZIndex: 9,
  365. list: []
  366. });
  367. that.$easyMap.initShape({
  368. map: state.map,
  369. layerName: "form-track-point",
  370. layerZIndex: 10,
  371. list: []
  372. });
  373. console.log(result)
  374. axios.post("/init-speed-track-api/hujie-track-server/mock", [result], {
  375. contentType: "application/json"
  376. }).then(res => {
  377. console.log(res)
  378. })
  379. }
  380. const trackShowListCom = computed(() => {
  381. return state.trackList.filter((v: { show: any; }) => v.show)
  382. })
  383. const initTrack = () => {
  384. that.$easyMap.initShape({
  385. map: state.map,
  386. layerName: "track-point-line",
  387. layerZIndex: 7,
  388. list: []
  389. });
  390. that.$easyMap.initShape({
  391. map: state.map,
  392. layerName: "track-point",
  393. layerZIndex: 8,
  394. list: []
  395. });
  396. that.$easyMap.initShape({
  397. map: state.map,
  398. layerName: "track-point-line",
  399. layerZIndex: 7,
  400. list: trackShowListCom.value.map((v: any) => {
  401. const pMap = new Map()
  402. v.lines.forEach((p: {
  403. speed: any; type: any; lon: any; lat: any;
  404. }) => {
  405. const obj = {
  406. source: v.type,
  407. position: [p.lon, p.lat],
  408. speed: p.speed
  409. }
  410. pMap.set(`${p.lon}-${p.lat}`, obj)
  411. })
  412. return {
  413. easyMapParams: {
  414. id: v.ID,
  415. position: that.$easyMap.formatPosition.wptTwl(v.lines.map((c: { lon: any; lat: any; }) => that.$easyMap.formatPosition.cptTwpt([c.lon, c.lat]))),
  416. normalStyle: (f: any, r: any) => TrackStyle.trackLineStyle(f, r, state.map, SourceMap.get(v.type)?.color, pMap, (s, p) => {
  417. state.initTrackPointStartCount++
  418. setTimeout(() => {
  419. state.initTrackPointEndCount++
  420. state.initTrackPointList.push(...p)
  421. if (state.initTrackPointStartCount === state.initTrackPointEndCount) {
  422. that.$easyMap.initShape({
  423. map: state.map,
  424. layerName: 'track-point',
  425. layerZIndex: 8,
  426. list: state.initTrackPointList.map((v, i) => {
  427. return {
  428. easyMapParams: {
  429. id: `init-track-point-${v.ID}-${i}`,
  430. position: that.$easyMap.formatPosition.cptTwpt(v.position),
  431. normalStyle: TrackStyle.trackPointStyle(SourceMap.get(v.source).color, v.speed)
  432. }
  433. }
  434. })
  435. })
  436. state.initTrackPointStartCount = 0
  437. state.initTrackPointEndCount = 0
  438. state.initTrackPointList = []
  439. }
  440. }, 10)
  441. return s
  442. }),
  443. }
  444. }
  445. })
  446. });
  447. }
  448. const handleShow = (show: any, item: any) => {
  449. item.show = show
  450. initTrack()
  451. }
  452. return {
  453. ...toRefs(state),
  454. mapLoad,
  455. drawTrack,
  456. onPointFocus,
  457. SourceMap,
  458. onSubmit,
  459. handleShow
  460. }
  461. }
  462. })
  463. </script>
  464. <style lang="scss" scoped>
  465. .init-speed-track {
  466. width: 100%;
  467. height: 100%;
  468. position: relative;
  469. .map {
  470. width: 100%;
  471. height: 100vh;
  472. }
  473. .track {
  474. position: absolute;
  475. z-index: 20;
  476. top: 0;
  477. left: 0;
  478. .track-line {
  479. overflow-y: auto;
  480. max-height: 180px;
  481. .line {
  482. height: 20px;
  483. display: flex;
  484. align-items: center;
  485. .label {
  486. width: 90px;
  487. }
  488. }
  489. }
  490. .track-point {
  491. overflow-y: auto;
  492. max-height: 600px;
  493. .point {
  494. display: flex;
  495. .position {
  496. font-size: 12px;
  497. }
  498. border-bottom: 1px solid black;
  499. }
  500. }
  501. }
  502. }
  503. </style>