index - 副本 (2).vue 11 KB

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