index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <template>
  2. <view class="map-box">
  3. <search ref="searchRef" @handleSearch="handleSearch" v-if="showMap" />
  4. <tiandituMap ref="tiandituMapRefs" @handleSearch="$refs.searchRef.onSearchFocus()" @clickMap="handleClickMap"
  5. @moveMap="moveMapSearch" @onLoadTianDiTu="initMaps" @onSelect="selectPoint" :apiKey="apiKey"
  6. @handleMapSite="handleMapSite">
  7. </tiandituMap>
  8. <Touchbox ref="TouchboxRef" :maxHeight="0.8" v-if="showMap && siteListArr && siteListArr.length > 0"
  9. :zIndex="1001" @currentHeight="e => TouchHeight = e">
  10. <scroll-view :style="{'height':TouchHeight - 15 +'px' }" scroll-y="true" :show-scrollbar="false">
  11. <template v-for="item in siteListArr">
  12. <siteListModel :info="item" @checkSiteDetails="checkSiteDetails" />
  13. </template>
  14. </scroll-view>
  15. </Touchbox>
  16. <siteDetails ref="siteDetailsRef" />
  17. </view>
  18. </template>
  19. <script>
  20. import tools from '@/components/tiandituMap/tools.js'
  21. import search from "./model/search.vue"
  22. import siteListModel from "./model/siteList.vue";
  23. import { getMapCenterPoint_Api, getMapList_Api } from "@/api/map.js"
  24. import siteDetails from "./model/siteDetails.vue"
  25. import $config from "@/config/index.js"
  26. import { EventBus } from "@/utils/vueBus.js"
  27. import { getLocation } from "@/utils/tool.js"
  28. export default {
  29. name: 'tdtmap',
  30. components: {
  31. search,
  32. siteListModel,
  33. siteDetails
  34. },
  35. data() {
  36. return {
  37. searchDot: false,
  38. showMap: false,
  39. longitude: undefined,
  40. latitude: undefined,
  41. apiKey: $config.tianKey,
  42. winWidth: 0,
  43. winHeight: 0,
  44. winTop: 0,
  45. datalist: [],
  46. startY: 0,
  47. selectItem: {},
  48. iStatusBarHeight: 0,
  49. siteListArr: [],
  50. TouchHeight: 0,
  51. }
  52. },
  53. created() {
  54. getLocation();
  55. this.getMapCenterPoint()
  56. },
  57. mounted() {
  58. this.disableScroll()
  59. },
  60. beforeDestroy() {
  61. try {
  62. document.removeEventListener('touchmove');
  63. document.body.removeEventListener('mousewheel');
  64. document.body.removeEventListener('DOMMouseScroll');
  65. document.removeEventListener('onmousewheel', function() {
  66. window.event.returnValue = false;
  67. });
  68. } catch (error) {
  69. //TODO handle the exception
  70. }
  71. },
  72. watch: {
  73. siteListArr: {
  74. handler(newArr) {
  75. try {
  76. this.$refs.tiandituMapRefs.clearIcon().then(res => {
  77. this.$nextTick(() => {
  78. (newArr || []).forEach((el, index) => {
  79. const { longitude, latitude, mapTypeIcon } = el
  80. this.$refs.tiandituMapRefs.setIcon(longitude, latitude, false,
  81. mapTypeIcon,
  82. el);
  83. if (index === 0 && this.searchDot) {
  84. this.$refs.tiandituMapRefs.Trenderjs.SelectedDot(el, true)
  85. }
  86. })
  87. })
  88. });
  89. } catch (error) {}
  90. },
  91. deep: true
  92. }
  93. },
  94. methods: {
  95. disableScroll() {
  96. if (typeof window.addEventListener === 'function') {
  97. document.addEventListener('touchmove', function(e) {
  98. e.preventDefault();
  99. }, { passive: false });
  100. document.body.addEventListener('mousewheel', function(e) {
  101. e.preventDefault();
  102. });
  103. document.body.addEventListener('DOMMouseScroll', function(e) {
  104. e.preventDefault();
  105. });
  106. } else {
  107. document.attachEvent('onmousewheel', function() {
  108. window.event.returnValue = false;
  109. });
  110. }
  111. },
  112. // 点击地图
  113. handleClickMap() {
  114. if (this.$refs.TouchboxRef) {
  115. this.$refs.TouchboxRef.concealList()
  116. }
  117. },
  118. // 点击地图标注点位
  119. handleMapSite(parmas = {}) {
  120. const { Lng, Lat } = parmas;
  121. const item = (this.siteListArr || []).find(el => el.longitude == Lng && el.latitude == Lat);
  122. this.checkSiteDetails(item)
  123. },
  124. // 查看点位详情
  125. checkSiteDetails(info) {
  126. if (info) {
  127. this.$refs.tiandituMapRefs.Trenderjs.SelectedDot(info, true)
  128. // EventBus.$emit('TianDiTuSite', info)
  129. this.handleClickMap();
  130. this.$refs.siteDetailsRef.openDetails(info)
  131. }
  132. },
  133. // 获取地图中心点
  134. getMapCenterPoint() {
  135. uni.showLoading()
  136. this.showMap = false;
  137. getMapCenterPoint_Api().then(res => {
  138. const { longitude, latitude, mapTypeIcon } = res || {};
  139. this.longitude = longitude;
  140. this.latitude = latitude;
  141. this.open(longitude, latitude, mapTypeIcon);
  142. }).catch(err => { this.open(null, null) }).finally(() => {
  143. uni.hideLoading()
  144. })
  145. },
  146. moveMapSearch(parms) {
  147. const { Lng, Lat } = parms;
  148. this.longitude = Lng;
  149. this.latitude = Lat;
  150. this.$refs.searchRef.onSearchFocus()
  151. // console.log("moveMapSearch", Lng, Lat)
  152. },
  153. handleSearch(val = {}) {
  154. const parms = {
  155. ...val,
  156. longitude: this.longitude,
  157. latitude: this.latitude,
  158. radius: 5000
  159. };
  160. // 判断是否通过收索
  161. this.searchDot = parms.search
  162. // 如果存在收索条件,则去掉 范围 和 中心点
  163. if (parms.locationName || parms.mapTypeId || parms.areaCode) {
  164. delete parms.radius
  165. delete parms.latitude
  166. delete parms.longitude
  167. }
  168. delete parms.search;
  169. getMapList_Api(parms).then(res => {
  170. if (!res || res.length === 0) {
  171. uni.showToast({
  172. title: "当前区域无办理点位",
  173. icon: 'none'
  174. })
  175. }
  176. this.siteListArr = res || [];
  177. }).catch(err => {
  178. this.siteListArr = [];
  179. }).finally(() => {
  180. clearTimeout(this.SearchTimeout)
  181. this.SearchTimeout = null;
  182. })
  183. },
  184. // //普通搜索
  185. // handleSearch(val = {}) {
  186. // const parms = {
  187. // ...val,
  188. // longitude: this.longitude,
  189. // latitude: this.latitude,
  190. // radius: 5000
  191. // };
  192. // getMapList_Api(parms).then(res => {
  193. // if (!res || res.length === 0) {
  194. // uni.showToast({
  195. // title: "当前区域无办理点位",
  196. // icon: 'none'
  197. // })
  198. // }
  199. // this.siteListArr = res || [];
  200. // }).catch(err => {
  201. // this.siteListArr = [];
  202. // })
  203. // },
  204. open(lon, lat, mapTypeIcon) {
  205. if (lon && lat) {
  206. this.$nextTick(() => {
  207. this.$refs.tiandituMapRefs.initCharts(lon, lat, mapTypeIcon)
  208. this.showMap = true;
  209. })
  210. } else {
  211. uni.showModal({
  212. title: '提示',
  213. content: '地图中心点获取错误,请联系管理员!',
  214. success: res => {
  215. // console.log("showModal == ", res)
  216. if (res.confirm) {}
  217. }
  218. })
  219. }
  220. },
  221. close() {
  222. this.visible = false
  223. },
  224. onConfirm() {
  225. if (Object.keys(this.selectItem).length) {
  226. this.visible = false
  227. this.$emit('onSelect', this.selectItem)
  228. } else {
  229. tools.createMessage('请选择位置')
  230. }
  231. },
  232. upDateLonLat(lon, lat) {
  233. if (lon && lat) {
  234. this.$refs.tiandituMapRefs.upDataCharts(lon, lat)
  235. } else {
  236. console.error('请传入lon, lat')
  237. }
  238. },
  239. tianidtuSearch(value) {
  240. if (value.city) {
  241. this.cityInfoSearch(value)
  242. } else {
  243. this.infoSearch(value)
  244. }
  245. },
  246. async infoSearch(value) { // 地理编码查询
  247. let params = {
  248. ds: {
  249. "keyWord": value.keyword,
  250. },
  251. tk: this.apiKey,
  252. }
  253. let resData = await tools.createRequest('https://api.tianditu.gov.cn/geocoder', params, true)
  254. if (resData.status === '0') {
  255. const location = resData.location
  256. const formateOne = tools.formatterAdressLocation(resData, 3)
  257. this.datalist = [formateOne]
  258. this.selectItem = datalist
  259. this.$refs.tiandituMapRefs.upDataCharts(location.lon, location.lat)
  260. }
  261. },
  262. async cityInfoSearch(value) { // 地名搜索2.0
  263. let params = {
  264. postStr: {
  265. "keyWord": value.keyword,
  266. "queryType": 12,
  267. "start": 0,
  268. "count": 10,
  269. "specify": value.city.value
  270. },
  271. type: 'query',
  272. tk: this.apiKey,
  273. }
  274. let resData = await tools.createRequest('https://api.tianditu.gov.cn/v2/search', params, true)
  275. if (resData.status.infocode === 1000) {
  276. const {
  277. pois: aPoints,
  278. count
  279. } = resData
  280. if (count === '0' || !aPoints || !aPoints.length) {
  281. return tools.createMessage('没有找到该地址')
  282. }
  283. const {
  284. pois,
  285. keyWord,
  286. lonlat
  287. } = aPoints[0]
  288. const formateData = aPoints.map((item) => tools.formatterAdressLocation(item, 2))
  289. this.datalist = formateData
  290. this.selectItem = formateData[0]
  291. const [lon, lat] = lonlat.split(',')
  292. this.$refs.tiandituMapRefs.upDataCharts(lon, lat)
  293. } else {
  294. tools.createMessage('数据异常', 1000, false, 'error')
  295. }
  296. },
  297. selectListItem(item) {
  298. this.$refs.tiandituMapRefs.upDataCharts(item.location.lon, item.location.lat)
  299. },
  300. selectPoint(e) {
  301. this.domMinHeight = '0vh'
  302. this.datalist = [e]
  303. this.selectItem = e
  304. },
  305. initMaps() {
  306. console.warn('--------天地图加载完成--------');
  307. this.$emit('onLoad')
  308. },
  309. // start(e) {
  310. // const clientY = e.changedTouches[0].clientY
  311. // this.startY = clientY
  312. // },
  313. // end(e) {
  314. // const transformY = e.changedTouches[0].clientY - this.startY;
  315. // switch (true) {
  316. // case transformY > 50:
  317. // console.log('下划')
  318. // this.domMaxHeight = '20vh'
  319. // this.domMinHeight = '0vh'
  320. // break;
  321. // case transformY < -50:
  322. // console.log('上划')
  323. // this.domMaxHeight = '50vh'
  324. // this.domMinHeight = '50vh'
  325. // break;
  326. // default:
  327. // break;
  328. // }
  329. // },
  330. selectCard(item) {
  331. this.domMaxHeight = '20vh'
  332. this.domMinHeight = '0vh'
  333. this.selectItem = item
  334. this.selectListItem(item)
  335. },
  336. setTouchHeight(val) {
  337. // console.log('setScrollHeight = ', val)
  338. // 实时返回的滑动组件高度
  339. this.TouchHeight = val;
  340. },
  341. setScrollMaxHeight(val) {
  342. //最大高度
  343. this.scrollMaxHeight = val;
  344. },
  345. }
  346. }
  347. </script>
  348. <style scope>
  349. .map-box {
  350. width: 100vw;
  351. height: 100vh;
  352. /* position: fixed;
  353. left: 0;
  354. top:0;
  355. right: 0;
  356. bottom: 0; */
  357. }
  358. .mask {
  359. /* overflow: hidden; */
  360. position: fixed;
  361. left: 0;
  362. background-color: #FFFFFF;
  363. z-index: 399;
  364. }
  365. /* footer */
  366. .list-boxd {
  367. position: absolute;
  368. bottom: 0;
  369. left: 0;
  370. z-index: 401;
  371. right: 0;
  372. border-radius: 14px 14px 0 0;
  373. background: #FFFFFF;
  374. transition: all 1s;
  375. }
  376. .list-header {
  377. height: 20px;
  378. position: relative;
  379. border-bottom: 1px solid #f3f4f6;
  380. cursor: pointer;
  381. }
  382. .list-header::after {
  383. position: absolute;
  384. left: 50%;
  385. top: 50%;
  386. transform: translate(-50%, -50%);
  387. content: '';
  388. height: 6px;
  389. width: 60px;
  390. border-top: 1px solid #e8e8e8;
  391. border-bottom: 1px solid #e8e8e8;
  392. }
  393. .list-content {
  394. max-height: 50vh;
  395. overflow-y: scroll;
  396. }
  397. .card {
  398. min-height: 44px;
  399. padding: 12px;
  400. position: relative;
  401. display: flex;
  402. justify-content: space-between;
  403. align-items: center;
  404. }
  405. .card-left {
  406. display: flex;
  407. flex-direction: column;
  408. justify-content: center;
  409. }
  410. .card-right {
  411. padding-right: 10px;
  412. }
  413. .arrow {
  414. border-top: 2px solid #666666;
  415. border-right: 2px solid #666666;
  416. width: 10px;
  417. height: 10px;
  418. transform: rotate(45deg);
  419. }
  420. .card:active {
  421. background-color: #f3f4f6;
  422. }
  423. .card::after {
  424. position: absolute;
  425. content: '';
  426. bottom: 0;
  427. height: 1px;
  428. background-color: #e8e8e8;
  429. width: 90%;
  430. }
  431. .card:last-child::after {
  432. height: 0;
  433. background-color: #FFFFFF;
  434. }
  435. .card-title {
  436. font-size: 18px;
  437. }
  438. .card-text {
  439. color: #e8e8e8;
  440. font-size: 13px;
  441. }
  442. </style>