index.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <template>
  2. <view class="map-box">
  3. <search />
  4. <tiandituMap ref="tiandituMapRefs" @onLoadTianDiTu="initMaps" @onSelect="selectPoint" :apiKey="apiKey">
  5. </tiandituMap>
  6. </view>
  7. </template>
  8. <script>
  9. import tools from '@/components/tiandituMap/tools.js'
  10. import search from "./model/search.vue"
  11. import { wgs84ToGCJ02 } from '@/components/tiandituMap/new_file.js'
  12. import { getMapCenterPoint_Api } from "@/api/map.js"
  13. export default {
  14. name: 'tdtmap',
  15. components: {
  16. // tiandituMap,
  17. search,
  18. },
  19. data() {
  20. return {
  21. apiKey: '1edd9c001a8425cb93631398109d5ab2',
  22. winWidth: 0,
  23. winHeight: 0,
  24. winTop: 0,
  25. datalist: [],
  26. startY: 0,
  27. domMaxHeight: '50vh',
  28. domMinHeight: '0vh',
  29. selectItem: {},
  30. iStatusBarHeight: 0,
  31. option: {
  32. apikey: '123123',
  33. }
  34. }
  35. },
  36. created() {
  37. // var that = this
  38. // uni.getSystemInfo({
  39. // success: function(res) {
  40. // that.winWidth = res.windowWidth
  41. // that.winHeight = res.windowHeight
  42. // that.winTop = res.windowTop
  43. // }
  44. // });
  45. },
  46. mounted() {
  47. // this.open(114.294, 30.534)
  48. this.getMapCenterPoint()
  49. },
  50. methods: {
  51. // 获取地图中心点
  52. getMapCenterPoint() {
  53. uni.showLoading()
  54. getMapCenterPoint_Api().then(res => {
  55. const { longitude, latitude } = res || {};
  56. this.open(longitude, latitude)
  57. }).catch(err => { this.open(null, null) }).finally(() => {
  58. uni.hideLoading()
  59. })
  60. },
  61. open(lon, lat) {
  62. if (lon && lat) {
  63. this.$nextTick(() => {
  64. this.$refs.tiandituMapRefs.initCharts(lon, lat)
  65. })
  66. } else {
  67. uni.showModal({
  68. title:'提示',
  69. content:'地图中心点获取错误,请联系管理员!',
  70. success:res => {
  71. console.log("showModal == " , res)
  72. if(res.confirm){}
  73. }
  74. })
  75. }
  76. },
  77. close() {
  78. this.visible = false
  79. },
  80. onConfirm() {
  81. if (Object.keys(this.selectItem).length) {
  82. this.visible = false
  83. this.$emit('onSelect', this.selectItem)
  84. } else {
  85. tools.createMessage('请选择位置')
  86. }
  87. },
  88. upDateLonLat(lon, lat) {
  89. if (lon && lat) {
  90. this.$refs.tiandituMapRefs.upDataCharts(lon, lat)
  91. } else {
  92. console.error('请传入lon, lat')
  93. }
  94. },
  95. tianidtuSearch(value) {
  96. if (value.city) {
  97. this.cityInfoSearch(value)
  98. } else {
  99. this.infoSearch(value)
  100. }
  101. },
  102. async infoSearch(value) { // 地理编码查询
  103. let params = {
  104. ds: {
  105. "keyWord": value.keyword,
  106. },
  107. tk: this.apiKey,
  108. }
  109. let resData = await tools.createRequest('http://api.tianditu.gov.cn/geocoder', params, true)
  110. if (resData.status === '0') {
  111. const location = resData.location
  112. const formateOne = tools.formatterAdressLocation(resData, 3)
  113. this.datalist = [formateOne]
  114. this.selectItem = datalist
  115. this.$refs.tiandituMapRefs.upDataCharts(location.lon, location.lat)
  116. }
  117. },
  118. async cityInfoSearch(value) { // 地名搜索2.0
  119. let params = {
  120. postStr: {
  121. "keyWord": value.keyword,
  122. "queryType": 12,
  123. "start": 0,
  124. "count": 10,
  125. "specify": value.city.value
  126. },
  127. type: 'query',
  128. tk: this.apiKey,
  129. }
  130. let resData = await tools.createRequest('http://api.tianditu.gov.cn/v2/search', params, true)
  131. if (resData.status.infocode === 1000) {
  132. const {
  133. pois: aPoints,
  134. count
  135. } = resData
  136. if (count === '0' || !aPoints || !aPoints.length) {
  137. return tools.createMessage('没有找到该地址')
  138. }
  139. const {
  140. pois,
  141. keyWord,
  142. lonlat
  143. } = aPoints[0]
  144. const formateData = aPoints.map((item) => tools.formatterAdressLocation(item, 2))
  145. this.datalist = formateData
  146. this.selectItem = formateData[0]
  147. const [lon, lat] = lonlat.split(',')
  148. this.$refs.tiandituMapRefs.upDataCharts(lon, lat)
  149. } else {
  150. tools.createMessage('数据异常', 1000, false, 'error')
  151. }
  152. },
  153. selectListItem(item) {
  154. this.$refs.tiandituMapRefs.upDataCharts(item.location.lon, item.location.lat)
  155. },
  156. selectPoint(e) {
  157. this.domMinHeight = '0vh'
  158. this.datalist = [e]
  159. this.selectItem = e
  160. },
  161. initMaps() {
  162. console.warn('--------天地图加载完成--------');
  163. this.$emit('onLoad')
  164. },
  165. start(e) {
  166. const clientY = e.changedTouches[0].clientY
  167. this.startY = clientY
  168. },
  169. end(e) {
  170. const transformY = e.changedTouches[0].clientY - this.startY;
  171. switch (true) {
  172. case transformY > 50:
  173. console.log('下划')
  174. this.domMaxHeight = '20vh'
  175. this.domMinHeight = '0vh'
  176. break;
  177. case transformY < -50:
  178. console.log('上划')
  179. this.domMaxHeight = '50vh'
  180. this.domMinHeight = '50vh'
  181. break;
  182. default:
  183. break;
  184. }
  185. },
  186. selectCard(item) {
  187. this.domMaxHeight = '20vh'
  188. this.domMinHeight = '0vh'
  189. this.selectItem = item
  190. this.selectListItem(item)
  191. }
  192. }
  193. }
  194. </script>
  195. <style scope>
  196. .map-box {
  197. width: 100vw;
  198. height: 100vh;
  199. }
  200. .mask {
  201. /* overflow: hidden; */
  202. position: fixed;
  203. left: 0;
  204. background-color: #FFFFFF;
  205. z-index: 399;
  206. }
  207. /* footer */
  208. .list-boxd {
  209. position: absolute;
  210. bottom: 0;
  211. left: 0;
  212. z-index: 401;
  213. right: 0;
  214. border-radius: 14px 14px 0 0;
  215. background: #FFFFFF;
  216. transition: all 1s;
  217. }
  218. .list-header {
  219. height: 20px;
  220. position: relative;
  221. border-bottom: 1px solid #f3f4f6;
  222. cursor: pointer;
  223. }
  224. .list-header::after {
  225. position: absolute;
  226. left: 50%;
  227. top: 50%;
  228. transform: translate(-50%, -50%);
  229. content: '';
  230. height: 6px;
  231. width: 60px;
  232. border-top: 1px solid #e8e8e8;
  233. border-bottom: 1px solid #e8e8e8;
  234. }
  235. .list-content {
  236. max-height: 50vh;
  237. overflow-y: scroll;
  238. }
  239. .card {
  240. min-height: 44px;
  241. padding: 12px;
  242. position: relative;
  243. display: flex;
  244. justify-content: space-between;
  245. align-items: center;
  246. }
  247. .card-left {
  248. display: flex;
  249. flex-direction: column;
  250. justify-content: center;
  251. }
  252. .card-right {
  253. padding-right: 10px;
  254. }
  255. .arrow {
  256. border-top: 2px solid #666666;
  257. border-right: 2px solid #666666;
  258. width: 10px;
  259. height: 10px;
  260. transform: rotate(45deg);
  261. }
  262. .card:active {
  263. background-color: #f3f4f6;
  264. }
  265. .card::after {
  266. position: absolute;
  267. content: '';
  268. bottom: 0;
  269. height: 1px;
  270. background-color: #e8e8e8;
  271. width: 90%;
  272. }
  273. .card:last-child::after {
  274. height: 0;
  275. background-color: #FFFFFF;
  276. }
  277. .card-title {
  278. font-size: 18px;
  279. }
  280. .card-text {
  281. color: #e8e8e8;
  282. font-size: 13px;
  283. }
  284. </style>