index.vue 9.4 KB

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