PositionMap.vue 12 KB

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