addLocation.vue 6.3 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/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. // console.log(item);
  145. let arr = uni.getStorageSync("chooseList")
  146. if (arr) {
  147. this.chooseList = uni.getStorageSync("chooseList")
  148. }
  149. this.chooseList.push({
  150. name: item.title,
  151. address: item.address,
  152. radius: this.rangeValue,
  153. lat: item.location.lat,
  154. lng: item.location.lng,
  155. })
  156. uni.setStorageSync("chooseList", this.chooseList)
  157. uni.navigateBack({
  158. delta: 1
  159. })
  160. },
  161. // 点击选择打卡范围回调
  162. changeRange() {
  163. uni.showActionSheet({
  164. itemList: this.rangeList,
  165. success: (res) => {
  166. this.rangeValue = this.rangeList[res.tapIndex]
  167. }
  168. });
  169. },
  170. // 搜索框失焦回调
  171. handleSearch(res) {
  172. this.placeList = []
  173. qqmapsdk.search({
  174. keyword: res,
  175. auto_extend: "0",
  176. success: (res) => {
  177. if (res.status == 0) {
  178. this.placeList = res.data
  179. } else {
  180. uni.showToast({
  181. title: "搜索位置失败",
  182. icon: 'none'
  183. })
  184. }
  185. },
  186. });
  187. },
  188. // 清除搜索框内容时的回调
  189. clear(res) {
  190. this.placeList = []
  191. },
  192. }
  193. }
  194. </script>
  195. <style lang="scss" scoped>
  196. .container {
  197. .map {
  198. position: relative;
  199. width: 100vw;
  200. height: 100vh;
  201. }
  202. .search {
  203. position: fixed;
  204. top: 30rpx;
  205. left: 30rpx;
  206. width: 690rpx;
  207. height: 80rpx;
  208. border-radius: 152rpx;
  209. background-color: #fff;
  210. }
  211. .list {
  212. position: fixed;
  213. left: 30rpx;
  214. bottom: 100rpx;
  215. padding: 0 30rpx;
  216. box-sizing: border-box;
  217. width: 690rpx;
  218. height: 435rpx;
  219. border-radius: 18rpx 18rpx 0 0;
  220. background-color: #fff;
  221. overflow-y: auto;
  222. .box {
  223. display: flex;
  224. align-items: center;
  225. width: 630rpx;
  226. height: 104rpx;
  227. border-bottom: 1rpx solid #E6E6E6;
  228. .icon {
  229. margin-top: 10rpx;
  230. width: 80rpx;
  231. text-align: center;
  232. img {
  233. width: 40rpx;
  234. height: 40rpx;
  235. }
  236. }
  237. .place {
  238. width: 550rpx;
  239. .top {
  240. font-size: 32rpx;
  241. overflow: hidden;
  242. white-space: nowrap;
  243. text-overflow: ellipsis;
  244. }
  245. .bottom {
  246. font-size: 24rpx;
  247. color: #999999;
  248. overflow: hidden;
  249. white-space: nowrap;
  250. text-overflow: ellipsis;
  251. }
  252. }
  253. }
  254. }
  255. .range {
  256. position: fixed;
  257. left: 0;
  258. bottom: 0;
  259. display: flex;
  260. // flex-direction: column;
  261. align-items: center;
  262. width: 750rpx;
  263. height: 100rpx;
  264. box-shadow: 1px -4px 10px 0px rgba(0, 0, 0, 0.2);
  265. background-color: #fff;
  266. .key {
  267. flex: 4;
  268. margin-left: 30rpx;
  269. font-size: 28rpx;
  270. }
  271. .value {
  272. flex: 1;
  273. font-size: 28rpx;
  274. color: #A6A6A6;
  275. img {
  276. margin-left: 15rpx;
  277. width: 20rpx;
  278. height: 25rpx;
  279. }
  280. }
  281. }
  282. }
  283. // 解决输入框不居中问题
  284. ::v-deep .uni-searchbar {
  285. padding: 10rpx;
  286. }
  287. ::v-deep .uni-searchbar__box {
  288. padding: 0;
  289. height: 60rpx;
  290. }
  291. </style>