index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. console.log('parms ', parms)
  170. getMapList_Api(parms).then(res => {
  171. if (!res || res.length === 0) {
  172. uni.showToast({
  173. title: "当前区域无办理点位",
  174. icon: 'none'
  175. })
  176. }
  177. this.siteListArr = res || [];
  178. }).catch(err => {
  179. this.siteListArr = [];
  180. }).finally(() => {
  181. clearTimeout(this.SearchTimeout)
  182. this.SearchTimeout = null;
  183. })
  184. },
  185. // //普通搜索
  186. // handleSearch(val = {}) {
  187. // const parms = {
  188. // ...val,
  189. // longitude: this.longitude,
  190. // latitude: this.latitude,
  191. // radius: 5000
  192. // };
  193. // getMapList_Api(parms).then(res => {
  194. // if (!res || res.length === 0) {
  195. // uni.showToast({
  196. // title: "当前区域无办理点位",
  197. // icon: 'none'
  198. // })
  199. // }
  200. // this.siteListArr = res || [];
  201. // }).catch(err => {
  202. // this.siteListArr = [];
  203. // })
  204. // },
  205. open(lon, lat, mapTypeIcon) {
  206. if (lon && lat) {
  207. this.$nextTick(() => {
  208. this.$refs.tiandituMapRefs.initCharts(lon, lat, mapTypeIcon)
  209. this.showMap = true;
  210. })
  211. } else {
  212. uni.showModal({
  213. title: '提示',
  214. content: '地图中心点获取错误,请联系管理员!',
  215. success: res => {
  216. // console.log("showModal == ", res)
  217. if (res.confirm) {}
  218. }
  219. })
  220. }
  221. },
  222. close() {
  223. this.visible = false
  224. },
  225. onConfirm() {
  226. if (Object.keys(this.selectItem).length) {
  227. this.visible = false
  228. this.$emit('onSelect', this.selectItem)
  229. } else {
  230. tools.createMessage('请选择位置')
  231. }
  232. },
  233. upDateLonLat(lon, lat) {
  234. if (lon && lat) {
  235. this.$refs.tiandituMapRefs.upDataCharts(lon, lat)
  236. } else {
  237. console.error('请传入lon, lat')
  238. }
  239. },
  240. tianidtuSearch(value) {
  241. if (value.city) {
  242. this.cityInfoSearch(value)
  243. } else {
  244. this.infoSearch(value)
  245. }
  246. },
  247. async infoSearch(value) { // 地理编码查询
  248. let params = {
  249. ds: {
  250. "keyWord": value.keyword,
  251. },
  252. tk: this.apiKey,
  253. }
  254. let resData = await tools.createRequest('https://api.tianditu.gov.cn/geocoder', params, true)
  255. if (resData.status === '0') {
  256. const location = resData.location
  257. const formateOne = tools.formatterAdressLocation(resData, 3)
  258. this.datalist = [formateOne]
  259. this.selectItem = datalist
  260. this.$refs.tiandituMapRefs.upDataCharts(location.lon, location.lat)
  261. }
  262. },
  263. async cityInfoSearch(value) { // 地名搜索2.0
  264. let params = {
  265. postStr: {
  266. "keyWord": value.keyword,
  267. "queryType": 12,
  268. "start": 0,
  269. "count": 10,
  270. "specify": value.city.value
  271. },
  272. type: 'query',
  273. tk: this.apiKey,
  274. }
  275. let resData = await tools.createRequest('https://api.tianditu.gov.cn/v2/search', params, true)
  276. if (resData.status.infocode === 1000) {
  277. const {
  278. pois: aPoints,
  279. count
  280. } = resData
  281. if (count === '0' || !aPoints || !aPoints.length) {
  282. return tools.createMessage('没有找到该地址')
  283. }
  284. const {
  285. pois,
  286. keyWord,
  287. lonlat
  288. } = aPoints[0]
  289. const formateData = aPoints.map((item) => tools.formatterAdressLocation(item, 2))
  290. this.datalist = formateData
  291. this.selectItem = formateData[0]
  292. const [lon, lat] = lonlat.split(',')
  293. this.$refs.tiandituMapRefs.upDataCharts(lon, lat)
  294. } else {
  295. tools.createMessage('数据异常', 1000, false, 'error')
  296. }
  297. },
  298. selectListItem(item) {
  299. this.$refs.tiandituMapRefs.upDataCharts(item.location.lon, item.location.lat)
  300. },
  301. selectPoint(e) {
  302. this.domMinHeight = '0vh'
  303. this.datalist = [e]
  304. this.selectItem = e
  305. },
  306. initMaps() {
  307. console.warn('--------天地图加载完成--------');
  308. this.$emit('onLoad')
  309. },
  310. // start(e) {
  311. // const clientY = e.changedTouches[0].clientY
  312. // this.startY = clientY
  313. // },
  314. // end(e) {
  315. // const transformY = e.changedTouches[0].clientY - this.startY;
  316. // switch (true) {
  317. // case transformY > 50:
  318. // console.log('下划')
  319. // this.domMaxHeight = '20vh'
  320. // this.domMinHeight = '0vh'
  321. // break;
  322. // case transformY < -50:
  323. // console.log('上划')
  324. // this.domMaxHeight = '50vh'
  325. // this.domMinHeight = '50vh'
  326. // break;
  327. // default:
  328. // break;
  329. // }
  330. // },
  331. selectCard(item) {
  332. this.domMaxHeight = '20vh'
  333. this.domMinHeight = '0vh'
  334. this.selectItem = item
  335. this.selectListItem(item)
  336. },
  337. setTouchHeight(val) {
  338. // console.log('setScrollHeight = ', val)
  339. // 实时返回的滑动组件高度
  340. this.TouchHeight = val;
  341. },
  342. setScrollMaxHeight(val) {
  343. //最大高度
  344. this.scrollMaxHeight = val;
  345. },
  346. }
  347. }
  348. </script>
  349. <style scope>
  350. .map-box {
  351. width: 100vw;
  352. height: 100vh;
  353. /* position: fixed;
  354. left: 0;
  355. top:0;
  356. right: 0;
  357. bottom: 0; */
  358. }
  359. .mask {
  360. /* overflow: hidden; */
  361. position: fixed;
  362. left: 0;
  363. background-color: #FFFFFF;
  364. z-index: 399;
  365. }
  366. /* footer */
  367. .list-boxd {
  368. position: absolute;
  369. bottom: 0;
  370. left: 0;
  371. z-index: 401;
  372. right: 0;
  373. border-radius: 14px 14px 0 0;
  374. background: #FFFFFF;
  375. transition: all 1s;
  376. }
  377. .list-header {
  378. height: 20px;
  379. position: relative;
  380. border-bottom: 1px solid #f3f4f6;
  381. cursor: pointer;
  382. }
  383. .list-header::after {
  384. position: absolute;
  385. left: 50%;
  386. top: 50%;
  387. transform: translate(-50%, -50%);
  388. content: '';
  389. height: 6px;
  390. width: 60px;
  391. border-top: 1px solid #e8e8e8;
  392. border-bottom: 1px solid #e8e8e8;
  393. }
  394. .list-content {
  395. max-height: 50vh;
  396. overflow-y: scroll;
  397. }
  398. .card {
  399. min-height: 44px;
  400. padding: 12px;
  401. position: relative;
  402. display: flex;
  403. justify-content: space-between;
  404. align-items: center;
  405. }
  406. .card-left {
  407. display: flex;
  408. flex-direction: column;
  409. justify-content: center;
  410. }
  411. .card-right {
  412. padding-right: 10px;
  413. }
  414. .arrow {
  415. border-top: 2px solid #666666;
  416. border-right: 2px solid #666666;
  417. width: 10px;
  418. height: 10px;
  419. transform: rotate(45deg);
  420. }
  421. .card:active {
  422. background-color: #f3f4f6;
  423. }
  424. .card::after {
  425. position: absolute;
  426. content: '';
  427. bottom: 0;
  428. height: 1px;
  429. background-color: #e8e8e8;
  430. width: 90%;
  431. }
  432. .card:last-child::after {
  433. height: 0;
  434. background-color: #FFFFFF;
  435. }
  436. .card-title {
  437. font-size: 18px;
  438. }
  439. .card-text {
  440. color: #e8e8e8;
  441. font-size: 13px;
  442. }
  443. </style>