PositionMap.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. <template>
  2. <div class="dialog">
  3. <Dialog type="md" :title="!!id ? '修改灾害信息' : '请选择事发地点'" draggable height="780px" custom-show @close="handleClose" @confirm="submit">
  4. <div class="custom-dialog">
  5. <el-form ref="queryFormRef" :model="form">
  6. <div class="form">
  7. <div class="line">
  8. <div class="form-item" style="margin-right: 20px">
  9. <div class="text">灾害事件</div>
  10. <el-input v-model="form.event_title" class="custom-input" placeholder="请输入" />
  11. </div>
  12. </div>
  13. <div class="line">
  14. <div class="form-item" style="margin-right: 20px">
  15. <div class="text">灾害级别</div>
  16. <el-select
  17. v-model="form.event_level"
  18. placeholder="请选择"
  19. class="custom-select"
  20. popper-class="custom-select-popper"
  21. :teleported="false"
  22. clearable
  23. >
  24. <el-option v-for="item in mm_event_level" :key="item.value" :label="item.label" :value="item.value" />
  25. </el-select>
  26. </div>
  27. </div>
  28. <div class="line">
  29. <div class="form-item" style="margin-right: 10px">
  30. <div class="text">详细地址</div>
  31. <el-input v-model="form.address" class="custom-input" placeholder="请输入" @input="handleInput" />
  32. <div v-if="searchPop" class="scroll_box" style="width: 640px; height: 580px">
  33. <div style="height: 30px; line-height: 30px">
  34. <span style="font-weight: bold">搜索结果列表</span>
  35. <i class="el-icon-close" style="float: right; font-size: 12px; cursor: pointer" @click="closeSearchList()" />
  36. </div>
  37. <el-scrollbar class="scroll">
  38. <div v-for="(item, index) in searchList" v-show="searchList.length" :key="index" class="item" @click="handlePanTo(index)">
  39. <el-image class="img" :src="item.img" :alt="item.name" lazy>
  40. <template #error>
  41. <div class="image-slot">
  42. <i class="el-icon-picture-outline"></i>
  43. </div>
  44. </template>
  45. </el-image>
  46. <div>
  47. <div class="text">{{ item.name }}</div>
  48. <div>{{ item.address }}</div>
  49. </div>
  50. </div>
  51. <div v-show="!searchList.length" class="empty" style="text-align: center">没有搜索到内容</div>
  52. </el-scrollbar>
  53. <el-pagination
  54. background
  55. small
  56. :hide-on-single-page="true"
  57. layout="prev, pager, next"
  58. :total="total"
  59. :page-size="pageSize"
  60. :current-page="pageNum"
  61. style="margin-top: 10px"
  62. @current-change="handleChangePage"
  63. >
  64. </el-pagination>
  65. </div>
  66. </div>
  67. <div class="common-btn-primary" @click="handleInput(0)">搜索</div>
  68. </div>
  69. <div class="line">
  70. <div class="form-item">
  71. <div class="text" style="width: 72px; text-align: right">经度</div>
  72. <el-input v-model="form.longitude" class="custom-input" placeholder="请输入" />
  73. </div>
  74. <div class="form-item">
  75. <div class="text" style="width: 72px; text-align: right">纬度</div>
  76. <el-input v-model="form.latitude" class="custom-input" placeholder="请输入" />
  77. </div>
  78. </div>
  79. </div>
  80. </el-form>
  81. <div ref="containerRef" class="map_box">
  82. <div
  83. id="map"
  84. class="map"
  85. :style="{
  86. width: width,
  87. height: height,
  88. transform: 'scale(' + inverseScale.inverseScaleX + ', ' + inverseScale.inverseScaleY + ')',
  89. transformOrigin: '0 0'
  90. }"
  91. />
  92. </div>
  93. </div>
  94. </Dialog>
  95. </div>
  96. </template>
  97. <script setup name="PositionMap">
  98. import AMapLoader from '@amap/amap-jsapi-loader';
  99. import { useRouter } from 'vue-router';
  100. import { addEvent } from '@/api/emergencyCommandMap/JointDuty';
  101. import { editEvent, getEventDetail } from '@/api/duty/eventing';
  102. const props = defineProps({
  103. id: {
  104. type: String,
  105. required: false
  106. },
  107. visible: {
  108. type: Boolean,
  109. default: () => {
  110. return false;
  111. }
  112. },
  113. flag: {
  114. type: Boolean,
  115. required: false
  116. }
  117. });
  118. const proxy = getCurrentInstance()?.proxy;
  119. const { mm_event_level } = toRefs(proxy?.useDict('mm_event_level'));
  120. const router = useRouter();
  121. const emits = defineEmits(['update:visible', 'confirm']);
  122. const containerScale = inject('containerScale');
  123. // 地图对象
  124. let map = null;
  125. let amap = {};
  126. let marker = null; //地图上的点标记
  127. let contextMenu = null;
  128. let lnglatPosition = ref([]); //选中的新坐标
  129. let pageNum = ref(1);
  130. let pageSize = ref(10);
  131. let total = ref(0);
  132. let searchList = ref([]);
  133. let searchPop = ref(false);
  134. let placeSearch;
  135. // 提交事件时间改为当前时间currentTime
  136. const now = new Date();
  137. const year = now.getFullYear();
  138. const month = String(now.getMonth() + 1).padStart(2, '0');
  139. const day = String(now.getDate()).padStart(2, '0');
  140. const hours = String(now.getHours()).padStart(2, '0');
  141. const minutes = String(now.getMinutes()).padStart(2, '0');
  142. const seconds = String(now.getSeconds()).padStart(2, '0');
  143. const currentTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  144. let form = ref({
  145. address: '',
  146. longitude: '',
  147. latitude: '',
  148. event_title: '',
  149. event_type: '',
  150. event_level: '',
  151. event_status: '1',
  152. event_time: currentTime,
  153. report_time: '1970-01-01 00:00:00',
  154. deaths: '0',
  155. injuries: '0',
  156. missing: '0',
  157. event_source: '',
  158. event_description: '',
  159. casualties: '',
  160. del_flag: '9' // 临时事件
  161. });
  162. let geocoder = {};
  163. let width = ref('100%');
  164. let height = ref('100%');
  165. watch(
  166. () => props.visible,
  167. (n) => {
  168. if (n) {
  169. nextTick(() => {
  170. if (props.id) {
  171. getEventDetail({ event_id: props.id }).then((res) => {
  172. form.value = res.data;
  173. initMap();
  174. });
  175. } else {
  176. initMap();
  177. }
  178. });
  179. }
  180. },
  181. {
  182. immediate: true
  183. }
  184. );
  185. onMounted(() => {
  186. window.addEventListener('resize', handleResize);
  187. });
  188. onUnmounted(() => {
  189. if (!!map) {
  190. map.off('rightclick');
  191. map.destroy();
  192. map = null;
  193. }
  194. window.removeEventListener('resize', handleResize);
  195. });
  196. function handleInput(flag) {
  197. if (!form.value.address) return;
  198. if (!flag) {
  199. //搜索
  200. total.value = 0;
  201. pageNum.value = 1;
  202. }
  203. if (!placeSearch) {
  204. placeSearch = new amap.PlaceSearch({
  205. pageSize: pageSize.value, // 每页条数,默认10,范围1-50
  206. pageIndex: pageNum.value, // 页码
  207. extensions: 'all' // 默认base,返回基本地址信息;all:返回详细信息
  208. });
  209. }
  210. searchPop.value = true;
  211. placeSearch.setPageIndex(pageNum.value);
  212. placeSearch.search(form.value.address, (status, result) => {
  213. // console.log(result.poiList.pois, 'result')
  214. if (!!result.poiList && result.poiList.pois && result.poiList.pois.length > 0) {
  215. let arr = [];
  216. const pois = result.poiList.pois;
  217. total.value = result.poiList ? result.poiList.count : 0;
  218. arr = pois.map((item) => {
  219. return {
  220. name: item.name,
  221. address: item.address,
  222. img: item.photos[0]?.url,
  223. lnglat: [item.location.lng, item.location.lat]
  224. };
  225. });
  226. searchList.value = arr;
  227. } else {
  228. total.value = 0;
  229. searchList.value = [];
  230. }
  231. });
  232. }
  233. function handleChangePage(newNum) {
  234. if (!searchPop.value) return;
  235. pageNum.value = newNum;
  236. handleInput(1);
  237. }
  238. function closeSearchList() {
  239. searchPop.value = false;
  240. searchList.value = [];
  241. total.value = 0;
  242. pageNum.value = 1;
  243. }
  244. // 地图中心的平移至指定点位置
  245. function handlePanTo(index) {
  246. let lnglat = searchList.value[index].lnglat;
  247. form.value.address = searchList.value[index].name + '(' + searchList.value[index].address + ')';
  248. form.value.longitude = lnglat[0];
  249. form.value.latitude = lnglat[1];
  250. map.panTo(lnglat);
  251. setMarks(lnglat);
  252. closeSearchList();
  253. }
  254. const updateForm = (position) => {
  255. form.value.longitude = position[0];
  256. form.value.latitude = position[1];
  257. geocoder.getAddress(position, (status, result) => {
  258. if (status === 'complete' && result.info === 'OK') {
  259. form.value.address = result.regeocode.formattedAddress;
  260. }
  261. });
  262. };
  263. const initMap = () => {
  264. let position = [110.925175, 21.678955];
  265. window._AMapSecurityConfig = {
  266. securityJsCode: '4868bc1b8fac7d9e54e7279ed556879a'
  267. };
  268. AMapLoader.load({
  269. key: '9c5041381e5e824f9ee324d8f7a40150', // 申请好的Web端开发者Key,首次调用 load 时必填
  270. version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  271. plugins: ['AMap.PlaceSearch', 'AMap.ContextMenu', 'AMap.PolygonEditor', 'AMap.Geocoder'] // 插件列表
  272. }).then((res) => {
  273. AMap = res;
  274. map = new AMap.Map('map', {
  275. viewMode: '3D', //是否为3D地图模式
  276. center: position,
  277. zoom: 15
  278. });
  279. amap = AMap;
  280. geocoder = new AMap.Geocoder({
  281. // city 指定进行编码查询的城市,支持传入城市名、adcode 和 citycode
  282. city: '010'
  283. });
  284. // 创建右键菜单
  285. ContextMenu();
  286. map.on('rightclick', handleRightclick);
  287. handleResize();
  288. map.on('complete', () => {
  289. if (!props.id) {
  290. updateForm(position);
  291. }
  292. setMarks(position);
  293. });
  294. });
  295. };
  296. function ContextMenu() {
  297. contextMenu = new AMap.ContextMenu();
  298. contextMenu.addItem(
  299. '选择标点',
  300. () => {
  301. form.value.longitude = lnglatPosition.value[0];
  302. form.value.latitude = lnglatPosition.value[1];
  303. contextMenu.close();
  304. const lnglat = [form.value.longitude, form.value.latitude];
  305. updateForm(lnglat);
  306. setMarks(lnglat);
  307. },
  308. 1
  309. );
  310. }
  311. // 右键事件
  312. function handleRightclick(e) {
  313. let lnglat = [e.lnglat.getLng(), e.lnglat.getLat()];
  314. contextMenu.open(map, e.lnglat);
  315. lnglatPosition.value = lnglat;
  316. }
  317. function setMarks(lnglat) {
  318. //添加标记
  319. if (marker) map.remove(marker);
  320. marker = new AMap.Marker({
  321. position: lnglat,
  322. icon: new AMap.Icon({
  323. size: new AMap.Size(22, 28), //图标所处区域大小
  324. imageSize: new AMap.Size(22, 28), //图标大小
  325. image: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png'
  326. }),
  327. draggable: true,
  328. anchor: 'bottom-center',
  329. offset: new AMap.Pixel(0, 0)
  330. });
  331. marker.on('dragend', (e) => {
  332. updateForm([e.lnglat.lng, e.lnglat.lat]);
  333. });
  334. marker.setMap(map);
  335. }
  336. function handleClose() {
  337. emits('update:visible', false);
  338. }
  339. let queryFormRef = ref();
  340. let containerRef = ref();
  341. let inverseScale = ref({
  342. inverseScaleX: 1,
  343. inverseScaleY: 1
  344. });
  345. function handleResize() {
  346. inverseScale.value.inverseScaleX = 1 / containerScale().scaleX;
  347. inverseScale.value.inverseScaleY = 1 / containerScale().scaleY;
  348. const containerWidth = containerRef.value.clientWidth * (document.body.clientWidth / 1920);
  349. const containerHeight = containerRef.value.clientHeight * (document.body.clientHeight / 1080);
  350. width.value = containerWidth + 'px';
  351. height.value = containerHeight + 'px';
  352. map.resize();
  353. }
  354. function submit() {
  355. if (!form.value.address) {
  356. proxy.$modal.msgError('详细地址不能为空');
  357. } else if (!form.value.longitude) {
  358. proxy.$modal.msgError('经度不能为空');
  359. } else if (!form.value.latitude) {
  360. proxy.$modal.msgError('纬度不能为空');
  361. } else {
  362. if (!!props.id) {
  363. let temp = {};
  364. temp.address = form.value.address;
  365. temp.casualties = form.value.casualties;
  366. temp.contact = form.value.contact;
  367. temp.create_time = form.value.create_time;
  368. temp.deaths = form.value.deaths;
  369. temp.del_flag = form.value.del_flag;
  370. temp.eventId = form.value.event_id;
  371. temp.event_code = temp.eventId;
  372. temp.description = form.value.description;
  373. temp.event_level = form.value.event_level;
  374. temp.event_source = form.value.event_source;
  375. temp.event_status = form.value.event_status;
  376. temp.event_time = form.value.event_time;
  377. temp.event_title = form.value.event_title;
  378. temp.event_type = form.value.event_type;
  379. temp.id = form.value.id;
  380. temp.injuries = form.value.injuries;
  381. temp.latitude = form.value.latitude;
  382. temp.longitude = form.value.longitude;
  383. temp.missing = form.value.missing;
  384. temp.plan_id = form.value.plan_id;
  385. temp.recorded_by = form.value.recorded_by;
  386. temp.region_code = form.value.region_code;
  387. temp.report_time = form.value.report_time;
  388. temp.response_level = form.value.response_level;
  389. editEvent(temp).then(() => {
  390. proxy.$modal.msgSuccess('修改成功');
  391. emits('update:visible', false);
  392. emits('confirm');
  393. });
  394. } else {
  395. addEvent(form.value).then((res) => {
  396. router.push({
  397. path: '/emergencyCommandMap',
  398. query: {
  399. event_id: res.data
  400. }
  401. });
  402. });
  403. }
  404. }
  405. }
  406. </script>
  407. <style lang="scss" scoped>
  408. .map_box {
  409. position: relative;
  410. background: rgba(0, 0, 0, 0.3);
  411. margin-bottom: 20px;
  412. flex: 1;
  413. .positionMap {
  414. width: 100%;
  415. height: 100%;
  416. }
  417. :deep(.amap-menu) {
  418. color: #000;
  419. }
  420. }
  421. .map {
  422. width: 100%;
  423. height: 100%;
  424. }
  425. .search {
  426. width: 50%;
  427. position: absolute;
  428. right: 2%;
  429. top: 10px;
  430. background: #fff;
  431. padding: 8px 8px;
  432. border-radius: 3px;
  433. display: flex;
  434. }
  435. .btn {
  436. margin-left: 10px;
  437. }
  438. .scroll_box {
  439. width: calc(100% - 65px);
  440. background: #0a2c5c;
  441. position: absolute;
  442. left: 66px;
  443. top: 38px;
  444. z-index: 9;
  445. padding: 10px;
  446. border-radius: 3px;
  447. .close {
  448. position: absolute;
  449. right: 10px;
  450. top: 10px;
  451. cursor: pointer;
  452. font-size: 14px;
  453. }
  454. :deep(.el-pagination__total) {
  455. color: #a7ccdf !important;
  456. }
  457. :deep(.el-pagination) {
  458. .btn-next,
  459. .btn-prev {
  460. background-color: transparent !important;
  461. border: none !important;
  462. .el-icon {
  463. color: #a7ccdf !important;
  464. }
  465. }
  466. .btn-prev:disabled,
  467. .btn-next:disabled {
  468. background-color: transparent !important;
  469. border: none !important;
  470. }
  471. .el-pager li {
  472. text-align: center;
  473. color: #a7ccdf !important;
  474. background-color: #0e3064 !important;
  475. border: 1px solid #0c57a7 !important;
  476. &:hover {
  477. background-color: #038dff !important;
  478. border: 1px solid #038dff !important;
  479. }
  480. }
  481. .el-pager li.is-active {
  482. background-color: #038dff !important;
  483. border: 1px solid #038dff !important;
  484. }
  485. }
  486. }
  487. .scroll {
  488. width: 100%;
  489. height: 500px;
  490. .item {
  491. display: flex;
  492. font-size: 14px;
  493. cursor: pointer;
  494. padding: 8px;
  495. &:hover {
  496. background-color: #102e76;
  497. }
  498. .img {
  499. width: 60px;
  500. height: 60px;
  501. min-width: 60px;
  502. margin-right: 8px;
  503. }
  504. :deep(.image-slot) {
  505. width: 100%;
  506. height: 100%;
  507. background-color: #f5f7fa;
  508. text-align: center;
  509. line-height: 50px;
  510. }
  511. :deep(.el-icon-picture-outline) {
  512. font-size: 32px;
  513. }
  514. .text {
  515. color: #3385ff;
  516. margin-bottom: 6px;
  517. }
  518. }
  519. }
  520. :deep(.el-scrollbar__wrap) {
  521. overflow-x: hidden !important;
  522. }
  523. :deep(.is-horizontal) {
  524. display: none;
  525. }
  526. .empty {
  527. margin: 20px 0;
  528. }
  529. .custom-dialog {
  530. height: 100%;
  531. padding: 10px 0;
  532. display: flex;
  533. flex-direction: column;
  534. .map_box {
  535. overflow: hidden;
  536. }
  537. }
  538. .form {
  539. .line {
  540. width: 100%;
  541. display: flex;
  542. align-items: center;
  543. margin-bottom: 10px;
  544. .form-item {
  545. flex: 1;
  546. display: flex;
  547. align-items: center;
  548. position: relative;
  549. .text {
  550. font-size: 14px;
  551. color: #eaf3fc;
  552. white-space: nowrap;
  553. margin-right: 10px;
  554. }
  555. }
  556. }
  557. }
  558. :deep(.amap-logo),
  559. :deep(.amap-copyright) {
  560. display: none !important;
  561. }
  562. .dialog {
  563. font-size: 38px;
  564. :deep(.dialog-content) {
  565. overflow: unset !important;
  566. }
  567. }
  568. .custom-pagination-popper {
  569. z-index: 9999 !important;
  570. position: relative;
  571. }
  572. </style>