index.vue 11 KB

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