addLocation.vue 6.2 KB

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