addLocation.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <template>
  2. <view class="container">
  3. <!-- 地图区域 -->
  4. <view class="map">
  5. <map style="width: 100%; height: 100%;" :latitude="latitude" :longitude="longitude" :scale="16"
  6. :markers="covers" @tap="handleClick">
  7. </map>
  8. </view>
  9. <!-- 顶部搜索框区域 -->
  10. <view class="search">
  11. <uni-search-bar bgColor="#fff" placeholder="请输入搜索地点" cancelButton="none" v-model="searchValue"
  12. @clear="clear" @input="handleSearch">
  13. </uni-search-bar>
  14. </view>
  15. <!-- 位置列表区域 -->
  16. <view class="list" v-if="placeList.length">
  17. <!-- 每一个位置区域 -->
  18. <view class="box" v-for="item in placeList" :key="item.id" @click="handleChoose(item)">
  19. <view class="icon">
  20. <img src="../static/imgs/location2.png">
  21. </view>
  22. <view class="place">
  23. <view class="top">
  24. {{item.title}}
  25. </view>
  26. <view class="bottom">
  27. 地点:{{item.address}}
  28. </view>
  29. </view>
  30. </view>
  31. </view>
  32. <!-- 打卡范围区域 -->
  33. <view class="range" v-if="placeList.length">
  34. <view class="key">
  35. 打卡范围:
  36. </view>
  37. <view class="value" @click="changeRange">
  38. <text>{{rangeValue}}米</text>
  39. <img src="../static/imgs/right.png">
  40. </view>
  41. </view>
  42. </view>
  43. </template>
  44. <script>
  45. var QQMapWX = require('../util/qqmap-wx-jssdk1.1/qqmap-wx-jssdk');
  46. var qqmapsdk;
  47. export default {
  48. data() {
  49. return {
  50. // 当前位置的经纬度
  51. latitude: null,
  52. longitude: null,
  53. // 标记点的配置
  54. covers: [{
  55. id: 1,
  56. latitude: null,
  57. longitude: null,
  58. iconPath: '../static/imgs/location.png',
  59. width: 20,
  60. height: 20,
  61. callout: {
  62. content: "",
  63. display: "ALWAYS"
  64. }
  65. }],
  66. // 搜索框绑定的值
  67. searchValue: "",
  68. // 范围选择数组
  69. rangeList: ['100', '300', '500'],
  70. // 搜索地点数组
  71. placeList: [],
  72. // 范围数值
  73. rangeValue: 100,
  74. // 选中的地点数组
  75. chooseList: [],
  76. }
  77. },
  78. onLoad() {
  79. // 实例化API核心类
  80. qqmapsdk = new QQMapWX({
  81. // 申请的key
  82. key: 'X57BZ-ZISE3-KTN3O-3P45H-C3J7Q-D5B67'
  83. });
  84. this.getLocationData()
  85. },
  86. methods: {
  87. // 获取当前位置信息
  88. getLocationData() {
  89. qqmapsdk.reverseGeocoder({
  90. success: (res) => {
  91. // console.log(res);
  92. if (res.status == 0) {
  93. // 获取地址经纬度
  94. this.latitude = res.result.location.lat
  95. this.longitude = res.result.location.lng
  96. // 获取标记点地址经纬度
  97. this.covers[0].latitude = res.result.location.lat
  98. this.covers[0].longitude = res.result.location.lng
  99. // 获取详细地址信息
  100. this.covers[0].callout.content = res.result.address
  101. } else {
  102. uni.showToast({
  103. title: "请求定位失败",
  104. icon: 'none'
  105. })
  106. }
  107. }
  108. })
  109. },
  110. // 点击地图回调事件
  111. handleClick(res) {
  112. this.placeList = []
  113. let {
  114. latitude
  115. } = res.detail
  116. let {
  117. longitude
  118. } = res.detail
  119. this.covers[0].latitude = latitude
  120. this.covers[0].longitude = longitude
  121. qqmapsdk.reverseGeocoder({
  122. location: {
  123. latitude,
  124. longitude
  125. },
  126. get_poi: 1,
  127. poi_options: "policy=2",
  128. success: (res) => {
  129. // console.log(res);
  130. if (res.status == 0) {
  131. this.placeList = res.result.pois
  132. this.covers[0].callout.content = res.result.address
  133. } else {
  134. uni.showToast({
  135. title: "请求定位失败",
  136. icon: 'none'
  137. })
  138. }
  139. }
  140. })
  141. },
  142. // 选择单个地址时的回调
  143. handleChoose(item) {
  144. let arr = uni.getStorageSync("chooseList")
  145. if (arr.length) {
  146. this.chooseList = arr
  147. }
  148. this.chooseList.push({
  149. name: item.title,
  150. address: item.address,
  151. radius: this.rangeValue,
  152. lat: item.location.lat,
  153. lng: item.location.lng,
  154. })
  155. uni.setStorageSync("chooseList", this.chooseList)
  156. uni.navigateBack({
  157. delta: 1
  158. })
  159. },
  160. // 点击选择打卡范围回调
  161. changeRange() {
  162. uni.showActionSheet({
  163. itemList: this.rangeList,
  164. success: (res) => {
  165. this.rangeValue = this.rangeList[res.tapIndex]
  166. }
  167. });
  168. },
  169. // 搜索框失焦回调
  170. handleSearch(res) {
  171. this.placeList = []
  172. qqmapsdk.search({
  173. keyword: res,
  174. auto_extend: "0",
  175. success: (res) => {
  176. if (res.status == 0) {
  177. this.placeList = res.data
  178. } else {
  179. uni.showToast({
  180. title: "搜索位置失败",
  181. icon: 'none'
  182. })
  183. }
  184. },
  185. });
  186. },
  187. // 清除搜索框内容时的回调
  188. clear(res) {
  189. this.placeList = []
  190. },
  191. }
  192. }
  193. </script>
  194. <style lang="scss" scoped>
  195. .container {
  196. .map {
  197. position: relative;
  198. width: 100vw;
  199. height: 100vh;
  200. }
  201. .search {
  202. position: fixed;
  203. top: 30rpx;
  204. left: 30rpx;
  205. width: 690rpx;
  206. height: 80rpx;
  207. border-radius: 152rpx;
  208. background-color: #fff;
  209. }
  210. .list {
  211. position: fixed;
  212. left: 30rpx;
  213. bottom: 100rpx;
  214. padding: 0 30rpx;
  215. box-sizing: border-box;
  216. width: 690rpx;
  217. height: 435rpx;
  218. border-radius: 18rpx 18rpx 0 0;
  219. background-color: #fff;
  220. overflow-y: auto;
  221. .box {
  222. display: flex;
  223. align-items: center;
  224. width: 630rpx;
  225. height: 104rpx;
  226. border-bottom: 1rpx solid #E6E6E6;
  227. .icon {
  228. margin-top: 10rpx;
  229. width: 80rpx;
  230. text-align: center;
  231. img {
  232. width: 40rpx;
  233. height: 40rpx;
  234. }
  235. }
  236. .place {
  237. width: 550rpx;
  238. .top {
  239. font-size: 32rpx;
  240. overflow: hidden;
  241. white-space: nowrap;
  242. text-overflow: ellipsis;
  243. }
  244. .bottom {
  245. font-size: 24rpx;
  246. color: #999999;
  247. overflow: hidden;
  248. white-space: nowrap;
  249. text-overflow: ellipsis;
  250. }
  251. }
  252. }
  253. }
  254. .range {
  255. position: fixed;
  256. left: 0;
  257. bottom: 0;
  258. display: flex;
  259. // flex-direction: column;
  260. align-items: center;
  261. width: 750rpx;
  262. height: 100rpx;
  263. box-shadow: 1px -4px 10px 0px rgba(0, 0, 0, 0.2);
  264. background-color: #fff;
  265. .key {
  266. flex: 4;
  267. margin-left: 30rpx;
  268. font-size: 28rpx;
  269. }
  270. .value {
  271. flex: 1;
  272. font-size: 28rpx;
  273. color: #A6A6A6;
  274. img {
  275. margin-left: 15rpx;
  276. width: 20rpx;
  277. height: 25rpx;
  278. }
  279. }
  280. }
  281. }
  282. // 解决输入框不居中问题
  283. ::v-deep .uni-searchbar {
  284. padding: 10rpx;
  285. }
  286. ::v-deep .uni-searchbar__box {
  287. padding: 0;
  288. height: 60rpx;
  289. }
  290. </style>