addLocation.vue 6.7 KB

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