PositionMap.vue 14 KB

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