PositionMap.vue 15 KB

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