addLocation.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <template>
  2. <view class="container">
  3. <!-- 地图区域 -->
  4. <view class="map"><map style="width: 100%; height: 100%;" :latitude="latitude" :longitude="longitude" :scale="16" :markers="covers"></map></view>
  5. <!-- 顶部搜索框区域 -->
  6. <view class="search">
  7. <uni-search-bar bgColor="#fff" placeholder="请输入搜索地点" cancelButton="none" v-model="searchValue" @clear="clear" @input="handleSearch"></uni-search-bar>
  8. </view>
  9. <!-- 位置列表区域 -->
  10. <view class="list" v-if="placeList.length">
  11. <!-- 每一个位置区域 -->
  12. <view class="box" v-for="item in placeList" :key="item.id" @click="handleChoose(item)">
  13. <view class="icon"><img src="../../static/imgs/location2.png" /></view>
  14. <view class="place">
  15. <view class="top">{{ item.title }}</view>
  16. <view class="bottom">地点:{{ item.address }}</view>
  17. </view>
  18. </view>
  19. </view>
  20. <!-- 打卡范围区域 -->
  21. <view class="range" v-if="placeList.length">
  22. <view class="key">打卡范围:</view>
  23. <view class="value" @click="changeRange">
  24. <text>{{ rangeValue }}米</text>
  25. <img src="../../static/imgs/right.png" />
  26. </view>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. export default {
  32. data() {
  33. return {
  34. // 当前位置的经纬度
  35. latitude: null,
  36. longitude: null,
  37. // 标记点的配置
  38. covers: [
  39. {
  40. id: 1,
  41. latitude: null,
  42. longitude: null,
  43. iconPath: '../../static/imgs/location.png',
  44. width: 30,
  45. height: 30,
  46. callout: {
  47. content: '',
  48. display: 'ALWAYS'
  49. }
  50. }
  51. ],
  52. // 搜索框绑定的值
  53. searchValue: '',
  54. // 范围选择数组
  55. rangeList: [100, 300, 500],
  56. // 搜索地点数组
  57. placeList: [],
  58. // 范围数值
  59. rangeValue: 100,
  60. // 选中的地点数组
  61. chooseList: [],
  62. // 定时器标识
  63. timer: null
  64. }
  65. },
  66. onLoad() {
  67. this.getLocationData()
  68. },
  69. methods: {
  70. // 获取当前位置信息
  71. getLocationData() {
  72. // 获取经纬度
  73. uni.getLocation({
  74. type: 'gcj02',
  75. success: res => {
  76. const KEY = 'X57BZ-ZISE3-KTN3O-3P45H-C3J7Q-D5B67'
  77. let url = 'https://apis.map.qq.com/ws/geocoder/v1'
  78. let locationdata = res.latitude + ',' + res.longitude
  79. this.$jsonp(url, {
  80. key: KEY,
  81. output: 'jsonp',
  82. location: locationdata
  83. })
  84. .then(json => {
  85. // console.log(json)
  86. if (json.status == 0) {
  87. // 获取详细地址信息 经纬度
  88. this.latitude = json.result.location.lat
  89. this.longitude = json.result.location.lng
  90. this.covers[0].latitude = json.result.location.lat
  91. this.covers[0].longitude = json.result.location.lng
  92. this.covers[0].callout.content = json.result.address
  93. } else {
  94. uni.showToast({
  95. title: `${json.message},请求定位失败`,
  96. icon: 'none'
  97. })
  98. }
  99. })
  100. .catch(err => {
  101. console.log(err)
  102. })
  103. },
  104. fail: error => {
  105. uni.showModal({
  106. title: '提示',
  107. content: '请先开启定位权限,否则将无法使用定位功能',
  108. cancelText: '不授权',
  109. confirmText: '授权',
  110. success: function(res) {
  111. if (res.confirm) {
  112. uni.openSetting({
  113. success(res) {}
  114. })
  115. } else if (res.cancel) {
  116. uni.redirectTo({
  117. url: '/pages/index/index'
  118. })
  119. }
  120. }
  121. })
  122. }
  123. })
  124. },
  125. // 选择单个地址时的回调
  126. handleChoose(item) {
  127. let arr = uni.getStorageSync('chooseList')
  128. if (arr.length) {
  129. this.chooseList = arr
  130. }
  131. this.chooseList.push({
  132. name: item.title,
  133. address: item.address,
  134. radius: this.rangeValue,
  135. lat: item.location.lat,
  136. lng: item.location.lng
  137. })
  138. uni.setStorageSync('chooseList', this.chooseList)
  139. uni.navigateBack({
  140. delta: 1
  141. })
  142. },
  143. // 点击选择打卡范围回调
  144. changeRange() {
  145. uni.showActionSheet({
  146. itemList: this.rangeList,
  147. success: res => {
  148. this.rangeValue = this.rangeList[res.tapIndex]
  149. }
  150. })
  151. },
  152. // 搜索框失焦回调
  153. handleSearch() {
  154. if (!this.searchValue) {
  155. return
  156. }
  157. if (this.timer) {
  158. clearTimeout(this.timer)
  159. }
  160. this.timer = setTimeout(() => {
  161. this.placeList = []
  162. const KEY = 'X57BZ-ZISE3-KTN3O-3P45H-C3J7Q-D5B67'
  163. let url = 'https://apis.map.qq.com/ws/place/v1/search'
  164. this.$jsonp(url, {
  165. key: KEY,
  166. output: 'jsonp',
  167. keyword: this.searchValue,
  168. boundary: `nearby(${this.latitude},${this.longitude},1000,1)`,
  169. page_size: 20
  170. })
  171. .then(json => {
  172. // console.log(json)
  173. if (json.status == 0) {
  174. this.placeList = json.data
  175. } else {
  176. uni.showToast({
  177. title: `${json.message},请求数据失败`,
  178. icon: 'none'
  179. })
  180. }
  181. })
  182. .catch(err => {
  183. console.log(err)
  184. })
  185. this.timer = null
  186. }, 500)
  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>