index.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <template>
  2. <view>
  3. <map id="Mymap" class="Mymap" :longitude="choose.longitude" :latitude="choose.latitude" show-location scale="16"
  4. :markers="markers" />
  5. <view class="search-box">
  6. <picker v-if="area.show" :value="area.name" mode="region" level="city" @change="regionChange">
  7. <view class="picker">
  8. <view class="name">
  9. {{ area.name[1] }}
  10. </view>
  11. <view class="iconfont icon-a-wodetiaozhuan" />
  12. </view>
  13. </picker>
  14. <view style="flex: 1;margin-left: 10px;">
  15. <My_search @onSearch="onSearch" isInput />
  16. </view>
  17. </view>
  18. <My_listbox boxBackground="#fff" bottomHeight="70" pullDown="false">
  19. <view v-if="list.length == 0" style="padding-top: 6vw;">
  20. <u-empty mode="search" />
  21. </view>
  22. <view class="item" @click="changeChoose(item)"
  23. :style="{ background: choose.lonlat == item.lonlat ? '#F8FBFF' : '#fff' }" v-for="item in list"
  24. :key="item.lonlat" hover-class="navigator-hover">
  25. <view class="name">
  26. {{ item.name }}
  27. </view>
  28. <view class="address">
  29. {{ getCity(item) }}
  30. </view>
  31. </view>
  32. </My_listbox>
  33. <view class="footer">
  34. <view class="confirm" :class="choose.name ? '' : 'forbidden'" @click="choose.name ? submit() : ''"
  35. hover-class="navigator-hover">
  36. 确定
  37. </view>
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. import { tianditu } from "../../utils/tianditu.js"
  43. const api = new tianditu();
  44. export default {
  45. data() {
  46. return {
  47. keyWord: "",
  48. choose: {
  49. longitude: "",
  50. latitude: ""
  51. },
  52. area: {
  53. show: false,
  54. },
  55. markers: [],
  56. list: [],
  57. circumjacents: []
  58. }
  59. },
  60. onLoad(options) {
  61. this.getLocation(true).then(location => {
  62. this.choose = location;
  63. api.getPlace(location.longitude, location.latitude).then(place => {
  64. console.log("逆解析", place)
  65. if (place.msg == 'ok') {
  66. this.list = [{
  67. ...place.result.addressComponent,
  68. name: place.result.addressComponent.poi,
  69. lonlat: `${location.longitude},${location.latitude}`,
  70. ...location
  71. }]
  72. this.choose = this.list[0];
  73. this.markers = [{
  74. longitude: this.choose.longitude,
  75. latitude: this.choose.latitude
  76. }];
  77. this.area = {
  78. show: true,
  79. name: [place.result.addressComponent.province, place.result.addressComponent.city, place.result.addressComponent.county],
  80. specify: [place.result.addressComponent.province_code, place.result.addressComponent.city_code, place.result.addressComponent.county_code]
  81. }
  82. this.getCircumjacent(place.result);
  83. } else {
  84. uni.showModal({
  85. title: '提示',
  86. content: '获取位置失败',
  87. showCancel: false,
  88. })
  89. }
  90. })
  91. })
  92. uni.setNavigationBarTitle({
  93. title: options.title || '选择地点'
  94. });
  95. },
  96. methods: {
  97. getCircumjacent(place) {
  98. api.placeNameSearch({
  99. keyWord: place.addressComponent.road,
  100. queryRadius: "1000",
  101. pointLonlat: `${place.location.lon},${place.location.lat}`,
  102. queryType: 3,
  103. start: 0,
  104. count: 5,
  105. dataTypes: "公司,标志性建筑物,家装建材零售,家居用品,五金,电器零售,工业园",
  106. show: 2
  107. }).then(res => {
  108. console.log("地址", res)
  109. for (const key in res) {
  110. if (key != 'keyWord' && res[key].pois) {
  111. this.list = this.unique(this.list.concat(res[key].pois.map(v => {
  112. let lonlat = v.lonlat.split(",")
  113. v.longitude = lonlat[0]
  114. v.latitude = lonlat[1]
  115. return v
  116. })), 'lonlat')
  117. this.circumjacents = JSON.parse(JSON.stringify(this.list))
  118. }
  119. }
  120. })
  121. },
  122. regionChange({ detail }) {
  123. this.area = {
  124. show: true,
  125. name: detail.value,
  126. specify: detail.code.map(v => "156" + v),
  127. }
  128. if (this.keyWord) this.onSearch(this.keyWord)
  129. },
  130. unique(arr, key) {
  131. for (let i = 0; i < arr.length; i++) {
  132. for (let j = i + 1; j < arr.length; j++) {
  133. if (arr[i][key] == arr[j][key]) {
  134. arr.splice(j, 1);
  135. j--;
  136. };
  137. };
  138. };
  139. return arr;
  140. },
  141. changeChoose(item) {
  142. this.choose = item;
  143. this.markers = [{
  144. longitude: item.longitude,
  145. latitude: item.latitude
  146. }];
  147. },
  148. onSearch(keyWord) {
  149. this.keyWord = keyWord;
  150. if (!keyWord) {
  151. this.list = this.circumjacents;
  152. } else {
  153. api.placeNameSearch({
  154. keyWord: keyWord,
  155. specify: this.area.specify[1],
  156. queryType: 12,
  157. start: 0,
  158. count: 30,
  159. show: 2
  160. }).then(res => {
  161. console.log("搜索地址", res)
  162. if (res.pois) {
  163. this.list = this.unique(res.pois.map(v => {
  164. let lonlat = v.lonlat.split(",")
  165. v.longitude = lonlat[0]
  166. v.latitude = lonlat[1]
  167. return v
  168. }), 'lonlat')
  169. } else {
  170. this.list = [];
  171. this.markers = [];
  172. this.choose = {
  173. longitude: "",
  174. latitude: ""
  175. };
  176. }
  177. })
  178. }
  179. if (this.list.length) {
  180. this.choose = this.list[0];
  181. this.markers = [{
  182. longitude: this.choose.longitude,
  183. latitude: this.choose.latitude
  184. }];
  185. }
  186. },
  187. submit() {
  188. this.$Http.routeSelected(this.choose)
  189. }
  190. },
  191. }
  192. </script>
  193. <style lang="scss" scoped>
  194. .Mymap {
  195. width: 100vw;
  196. height: 37vh;
  197. }
  198. .search-box {
  199. display: flex;
  200. align-items: center;
  201. width: 100vw;
  202. padding: 10px;
  203. box-sizing: border-box;
  204. background: #fff;
  205. .picker {
  206. display: flex;
  207. flex-direction: row;
  208. align-items: center;
  209. .icon-a-wodetiaozhuan {
  210. transform: rotate(90deg);
  211. margin-left: 4px;
  212. }
  213. }
  214. }
  215. .item {
  216. width: 100vw;
  217. margin: 0 auto;
  218. // border-bottom: .5px solid #ddd;
  219. padding: 10px;
  220. box-sizing: border-box;
  221. .name {
  222. line-height: 20px;
  223. font-family: Source Han Sans SC, Source Han Sans SC;
  224. font-weight: bold;
  225. font-size: 14px;
  226. color: #333333;
  227. margin-bottom: 5px;
  228. }
  229. .address {
  230. line-height: 17px;
  231. font-family: Source Han Sans SC, Source Han Sans SC;
  232. font-size: 12px;
  233. color: #999999;
  234. }
  235. }
  236. .footer {
  237. position: fixed;
  238. bottom: 0;
  239. width: 100vw;
  240. height: 65px;
  241. background: #FFFFFF;
  242. box-shadow: 0px -2px 6px 1px rgba(0, 0, 0, 0.16);
  243. box-sizing: border-box;
  244. padding: 5px 10px;
  245. z-index: 2;
  246. .confirm {
  247. display: flex;
  248. align-items: center;
  249. justify-content: center;
  250. width: 100%;
  251. height: 45px;
  252. background: #C30D23;
  253. border-radius: 5px;
  254. font-family: PingFang SC, PingFang SC;
  255. font-size: 14px;
  256. color: #FFFFFF;
  257. }
  258. .forbidden {
  259. opacity: .6;
  260. }
  261. }
  262. </style>