company-map.vue 11 KB

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