index.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <template>
  2. <view class="map-box">
  3. <search @handleSearch="handleSearch" v-if="showMap" />
  4. <tiandituMap ref="tiandituMapRefs" @onLoadTianDiTu="initMaps" @onSelect="selectPoint" :apiKey="apiKey">
  5. </tiandituMap>
  6. <bab-Touchbox v-if="showMap && siteList && siteList.length > 0" :zIndex="1001" :minHeight="0.35" :maxHeight="0.8" :touchHeight="64"
  7. @currentHeight="setTouchHeight" @maxtHeight="setScrollMaxHeight">
  8. <scroll-view :style="{'height':TouchHeight - 40 +'px' }" scroll-y="true" :show-scrollbar="false">
  9. <template v-for="item in siteList">
  10. <siteListModel :info="item" />
  11. </template>
  12. </scroll-view>
  13. </bab-Touchbox>
  14. </view>
  15. </template>
  16. <script>
  17. import tools from '@/components/tiandituMap/tools.js'
  18. import search from "./model/search.vue"
  19. import siteListModel from "./model/siteList.vue";
  20. import { getMapCenterPoint_Api, getMapList_Api } from "@/api/map.js"
  21. export default {
  22. name: 'tdtmap',
  23. components: {
  24. // tiandituMap,
  25. search,
  26. siteListModel
  27. },
  28. data() {
  29. return {
  30. showMap: false,
  31. longitude: undefined,
  32. latitude: undefined,
  33. apiKey: '1edd9c001a8425cb93631398109d5ab2',
  34. winWidth: 0,
  35. winHeight: 0,
  36. winTop: 0,
  37. datalist: [],
  38. startY: 0,
  39. // domMaxHeight: '50vh',
  40. // domMinHeight: '0vh',
  41. selectItem: {},
  42. iStatusBarHeight: 0,
  43. option: {
  44. apikey: '123123',
  45. },
  46. siteList: [],
  47. TouchHeight: 0,
  48. }
  49. },
  50. created() {
  51. // if (navigator.geolocation) {
  52. // navigator.geolocation.getCurrentPosition(function(position) {
  53. // var latitude = position.coords.latitude;
  54. // var longitude = position.coords.longitude;
  55. // // 在这里处理获取到的经纬度
  56. // console.log("Longitude is :", latitude , longitude);
  57. // });
  58. // } else {
  59. // // 处理不支持 Geolocation 的情况
  60. // console.error("Error Code = " + error.code + " - " + error.message);
  61. // }
  62. // try {
  63. // uni.getLocation({
  64. // type:'wgs84',
  65. // cacheTimeout: 30 * 60,
  66. // success: Location => {
  67. // console.log("getLocation" , Location)
  68. // // const address = Location.address;
  69. // // // 缓存全局的定位信息
  70. // // app.globalData.location = address;
  71. // // this.location = address;
  72. // // if (!address.city) {
  73. // // uni.showToast({
  74. // // title: '获取定位失败',
  75. // // icon: 'none'
  76. // // })
  77. // // } else {
  78. // // uni.showToast({
  79. // // title: '获取定位成功,请开始抽奖',
  80. // // icon: 'none'
  81. // // })
  82. // // }
  83. // },
  84. // fail: LocationErr => {
  85. // console.log("LocationErr = " , LocationErr)
  86. // }
  87. // })
  88. // } catch (error) {
  89. // console.log("LocationErr = catch" , LocationErr)
  90. // }
  91. // // uni.getSystemInfo({
  92. // // success: function(res) {
  93. // // that.winWidth = res.windowWidth
  94. // // that.winHeight = res.windowHeight
  95. // // that.winTop = res.windowTop
  96. // // }
  97. // // });
  98. },
  99. mounted() {
  100. // this.open(114.294, 30.534)
  101. this.getMapCenterPoint()
  102. },
  103. watch: {
  104. siteList: {
  105. handler(newArr) {
  106. this.$refs.tiandituMapRefs.clearIcon();
  107. (newArr || []).forEach(el => {
  108. const { longitude, latitude, mapTypeIcon } = el
  109. this.$refs.tiandituMapRefs.setIcon(longitude, latitude, false, mapTypeIcon);
  110. })
  111. },
  112. deep: true
  113. }
  114. },
  115. methods: {
  116. // 获取地图中心点
  117. getMapCenterPoint() {
  118. uni.showLoading()
  119. this.showMap = false;
  120. getMapCenterPoint_Api().then(res => {
  121. const { longitude, latitude , mapTypeIcon } = res || {};
  122. this.longitude = longitude;
  123. this.latitude = latitude;
  124. this.open(longitude, latitude , mapTypeIcon)
  125. }).catch(err => { this.open(null, null) }).finally(() => {
  126. uni.hideLoading()
  127. })
  128. },
  129. handleSearch(val = {}) {
  130. const parms = { ...val, longitude: this.longitude, latitude: this.latitude };
  131. getMapList_Api(parms).then(res => {
  132. if(!res || res.length === 0){
  133. uni.showToast({
  134. title:"搜索结果为空",
  135. icon:'none'
  136. })
  137. }
  138. this.siteList = res || [];
  139. }).catch(err => {
  140. this.siteList = [];
  141. })
  142. },
  143. open(lon, lat , mapTypeIcon) {
  144. if (lon && lat) {
  145. this.$nextTick(() => {
  146. this.$refs.tiandituMapRefs.initCharts(lon, lat , mapTypeIcon)
  147. this.showMap = true;
  148. })
  149. } else {
  150. uni.showModal({
  151. title: '提示',
  152. content: '地图中心点获取错误,请联系管理员!',
  153. success: res => {
  154. console.log("showModal == ", res)
  155. if (res.confirm) {}
  156. }
  157. })
  158. }
  159. },
  160. close() {
  161. this.visible = false
  162. },
  163. onConfirm() {
  164. if (Object.keys(this.selectItem).length) {
  165. this.visible = false
  166. this.$emit('onSelect', this.selectItem)
  167. } else {
  168. tools.createMessage('请选择位置')
  169. }
  170. },
  171. upDateLonLat(lon, lat) {
  172. if (lon && lat) {
  173. this.$refs.tiandituMapRefs.upDataCharts(lon, lat)
  174. } else {
  175. console.error('请传入lon, lat')
  176. }
  177. },
  178. tianidtuSearch(value) {
  179. if (value.city) {
  180. this.cityInfoSearch(value)
  181. } else {
  182. this.infoSearch(value)
  183. }
  184. },
  185. async infoSearch(value) { // 地理编码查询
  186. let params = {
  187. ds: {
  188. "keyWord": value.keyword,
  189. },
  190. tk: this.apiKey,
  191. }
  192. let resData = await tools.createRequest('http://api.tianditu.gov.cn/geocoder', params, true)
  193. if (resData.status === '0') {
  194. const location = resData.location
  195. const formateOne = tools.formatterAdressLocation(resData, 3)
  196. this.datalist = [formateOne]
  197. this.selectItem = datalist
  198. this.$refs.tiandituMapRefs.upDataCharts(location.lon, location.lat)
  199. }
  200. },
  201. async cityInfoSearch(value) { // 地名搜索2.0
  202. let params = {
  203. postStr: {
  204. "keyWord": value.keyword,
  205. "queryType": 12,
  206. "start": 0,
  207. "count": 10,
  208. "specify": value.city.value
  209. },
  210. type: 'query',
  211. tk: this.apiKey,
  212. }
  213. let resData = await tools.createRequest('http://api.tianditu.gov.cn/v2/search', params, true)
  214. if (resData.status.infocode === 1000) {
  215. const {
  216. pois: aPoints,
  217. count
  218. } = resData
  219. if (count === '0' || !aPoints || !aPoints.length) {
  220. return tools.createMessage('没有找到该地址')
  221. }
  222. const {
  223. pois,
  224. keyWord,
  225. lonlat
  226. } = aPoints[0]
  227. const formateData = aPoints.map((item) => tools.formatterAdressLocation(item, 2))
  228. this.datalist = formateData
  229. this.selectItem = formateData[0]
  230. const [lon, lat] = lonlat.split(',')
  231. this.$refs.tiandituMapRefs.upDataCharts(lon, lat)
  232. } else {
  233. tools.createMessage('数据异常', 1000, false, 'error')
  234. }
  235. },
  236. selectListItem(item) {
  237. this.$refs.tiandituMapRefs.upDataCharts(item.location.lon, item.location.lat)
  238. },
  239. selectPoint(e) {
  240. this.domMinHeight = '0vh'
  241. this.datalist = [e]
  242. this.selectItem = e
  243. },
  244. initMaps() {
  245. console.warn('--------天地图加载完成--------');
  246. this.$emit('onLoad')
  247. },
  248. // start(e) {
  249. // const clientY = e.changedTouches[0].clientY
  250. // this.startY = clientY
  251. // },
  252. // end(e) {
  253. // const transformY = e.changedTouches[0].clientY - this.startY;
  254. // switch (true) {
  255. // case transformY > 50:
  256. // console.log('下划')
  257. // this.domMaxHeight = '20vh'
  258. // this.domMinHeight = '0vh'
  259. // break;
  260. // case transformY < -50:
  261. // console.log('上划')
  262. // this.domMaxHeight = '50vh'
  263. // this.domMinHeight = '50vh'
  264. // break;
  265. // default:
  266. // break;
  267. // }
  268. // },
  269. selectCard(item) {
  270. this.domMaxHeight = '20vh'
  271. this.domMinHeight = '0vh'
  272. this.selectItem = item
  273. this.selectListItem(item)
  274. },
  275. setTouchHeight(val) {
  276. console.log('setScrollHeight = ', val)
  277. // 实时返回的滑动组件高度
  278. this.TouchHeight = val;
  279. },
  280. setScrollMaxHeight(val) {
  281. //最大高度
  282. this.scrollMaxHeight = val;
  283. },
  284. }
  285. }
  286. </script>
  287. <style scope>
  288. .map-box {
  289. width: 100vw;
  290. height: 100vh;
  291. }
  292. .mask {
  293. /* overflow: hidden; */
  294. position: fixed;
  295. left: 0;
  296. background-color: #FFFFFF;
  297. z-index: 399;
  298. }
  299. /* footer */
  300. .list-boxd {
  301. position: absolute;
  302. bottom: 0;
  303. left: 0;
  304. z-index: 401;
  305. right: 0;
  306. border-radius: 14px 14px 0 0;
  307. background: #FFFFFF;
  308. transition: all 1s;
  309. }
  310. .list-header {
  311. height: 20px;
  312. position: relative;
  313. border-bottom: 1px solid #f3f4f6;
  314. cursor: pointer;
  315. }
  316. .list-header::after {
  317. position: absolute;
  318. left: 50%;
  319. top: 50%;
  320. transform: translate(-50%, -50%);
  321. content: '';
  322. height: 6px;
  323. width: 60px;
  324. border-top: 1px solid #e8e8e8;
  325. border-bottom: 1px solid #e8e8e8;
  326. }
  327. .list-content {
  328. max-height: 50vh;
  329. overflow-y: scroll;
  330. }
  331. .card {
  332. min-height: 44px;
  333. padding: 12px;
  334. position: relative;
  335. display: flex;
  336. justify-content: space-between;
  337. align-items: center;
  338. }
  339. .card-left {
  340. display: flex;
  341. flex-direction: column;
  342. justify-content: center;
  343. }
  344. .card-right {
  345. padding-right: 10px;
  346. }
  347. .arrow {
  348. border-top: 2px solid #666666;
  349. border-right: 2px solid #666666;
  350. width: 10px;
  351. height: 10px;
  352. transform: rotate(45deg);
  353. }
  354. .card:active {
  355. background-color: #f3f4f6;
  356. }
  357. .card::after {
  358. position: absolute;
  359. content: '';
  360. bottom: 0;
  361. height: 1px;
  362. background-color: #e8e8e8;
  363. width: 90%;
  364. }
  365. .card:last-child::after {
  366. height: 0;
  367. background-color: #FFFFFF;
  368. }
  369. .card-title {
  370. font-size: 18px;
  371. }
  372. .card-text {
  373. color: #e8e8e8;
  374. font-size: 13px;
  375. }
  376. </style>