addLocation.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/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/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. latitude: null,
  51. longitude: null,
  52. covers: [{
  53. id: 1,
  54. latitude: null,
  55. longitude: null,
  56. iconPath: '../../static/location.png',
  57. width: 20,
  58. height: 20,
  59. callout: {
  60. content: "",
  61. display: "ALWAYS"
  62. }
  63. }],
  64. imgUrl: "",
  65. // 搜索框绑定的值
  66. searchValue: "",
  67. // 范围选择数组
  68. rangeList: ['300', '400', '500'],
  69. // 搜索地点数组
  70. placeList: [],
  71. // 范围数值
  72. rangeValue: 300,
  73. chooseList: []
  74. }
  75. },
  76. onLoad() {
  77. // 实例化API核心类
  78. qqmapsdk = new QQMapWX({
  79. // 申请的key
  80. key: 'R43BZ-2XROX-L7T45-T5OQI-IBDFT-GNBOI'
  81. });
  82. this.getLocationData()
  83. },
  84. methods: {
  85. // 获取当前位置信息
  86. getLocationData() {
  87. qqmapsdk.reverseGeocoder({
  88. success: (res) => {
  89. // console.log(res);
  90. // console.log(res.result);
  91. if (res.status == 0) {
  92. // 获取地址经纬度
  93. this.latitude = res.result.location.lat
  94. this.longitude = res.result.location.lng
  95. // 获取标记点地址经纬度
  96. this.covers[0].latitude = res.result.location.lat
  97. this.covers[0].longitude = res.result.location.lng
  98. // 获取详细地址信息
  99. this.covers[0].callout.content = res.result.address
  100. } else {
  101. uni.showToast({
  102. title: "请求定位失败",
  103. icon: 'none'
  104. })
  105. }
  106. }
  107. })
  108. },
  109. // 点击地图回调事件
  110. handleClick(res) {
  111. this.placeList = []
  112. let {
  113. latitude
  114. } = res.detail
  115. let {
  116. longitude
  117. } = res.detail
  118. this.covers[0].latitude = latitude
  119. this.covers[0].longitude = longitude
  120. qqmapsdk.reverseGeocoder({
  121. location: {
  122. latitude,
  123. longitude
  124. },
  125. get_poi: 1,
  126. poi_options: "policy=2",
  127. success: (res) => {
  128. // console.log(res.result);
  129. // console.log(res.result.pois);
  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) {
  146. this.chooseList = uni.getStorageSync("chooseList")
  147. }
  148. this.chooseList.push({
  149. title: item.title,
  150. address: item.address,
  151. scope: this.rangeValue
  152. })
  153. uni.setStorageSync("chooseList", this.chooseList)
  154. uni.navigateBack({
  155. delta: 1
  156. })
  157. },
  158. // 点击选择打卡范围回调
  159. changeRange() {
  160. uni.showActionSheet({
  161. itemList: this.rangeList,
  162. success: (res) => {
  163. this.rangeValue = this.rangeList[res.tapIndex]
  164. }
  165. });
  166. },
  167. // 搜索框失焦回调
  168. handleSearch(res) {
  169. this.placeList = []
  170. qqmapsdk.search({
  171. keyword: res,
  172. auto_extend: "0",
  173. success: (res) => {
  174. if (res.status == 0) {
  175. this.placeList = res.data
  176. } else {
  177. uni.showToast({
  178. title: "搜索位置失败",
  179. icon: 'none'
  180. })
  181. }
  182. },
  183. });
  184. },
  185. // 清除搜索框内容时的回调
  186. clear(res) {
  187. this.placeList = []
  188. },
  189. }
  190. }
  191. </script>
  192. <style lang="scss" scoped>
  193. .container {
  194. .map {
  195. position: relative;
  196. width: 100vw;
  197. height: 100vh;
  198. }
  199. .search {
  200. position: fixed;
  201. top: 30rpx;
  202. left: 30rpx;
  203. width: 690rpx;
  204. height: 80rpx;
  205. border-radius: 152rpx;
  206. background-color: #fff;
  207. }
  208. .list {
  209. position: fixed;
  210. left: 30rpx;
  211. bottom: 100rpx;
  212. padding: 0 30rpx;
  213. box-sizing: border-box;
  214. width: 690rpx;
  215. height: 435rpx;
  216. border-radius: 18rpx 18rpx 0 0;
  217. background-color: #fff;
  218. overflow-y: auto;
  219. .box {
  220. display: flex;
  221. align-items: center;
  222. width: 630rpx;
  223. height: 104rpx;
  224. border-bottom: 1rpx solid #E6E6E6;
  225. .icon {
  226. margin-top: 10rpx;
  227. width: 80rpx;
  228. text-align: center;
  229. img {
  230. width: 40rpx;
  231. height: 40rpx;
  232. }
  233. }
  234. .place {
  235. width: 550rpx;
  236. .top {
  237. font-size: 32rpx;
  238. overflow: hidden;
  239. white-space: nowrap;
  240. text-overflow: ellipsis;
  241. }
  242. .bottom {
  243. font-size: 24rpx;
  244. color: #999999;
  245. overflow: hidden;
  246. white-space: nowrap;
  247. text-overflow: ellipsis;
  248. }
  249. }
  250. }
  251. }
  252. .range {
  253. position: fixed;
  254. left: 0;
  255. bottom: 0;
  256. display: flex;
  257. // flex-direction: column;
  258. align-items: center;
  259. width: 750rpx;
  260. height: 100rpx;
  261. box-shadow: 1px -4px 10px 0px rgba(0, 0, 0, 0.2);
  262. background-color: #fff;
  263. .key {
  264. flex: 4;
  265. margin-left: 30rpx;
  266. font-size: 28rpx;
  267. }
  268. .value {
  269. flex: 1;
  270. font-size: 28rpx;
  271. color: #A6A6A6;
  272. img {
  273. margin-left: 15rpx;
  274. width: 20rpx;
  275. height: 25rpx;
  276. }
  277. }
  278. }
  279. }
  280. // 解决输入框不居中问题
  281. ::v-deep .uni-searchbar {
  282. padding: 10rpx;
  283. }
  284. ::v-deep .uni-searchbar__box {
  285. padding: 0;
  286. height: 60rpx;
  287. }
  288. </style>