position.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <template>
  2. <view class="">
  3. <view class="header">
  4. <view class="city Medium">
  5. <view class="select-city" @click="loadCity">{{city}}</view>
  6. <u-icon name="arrow-down"></u-icon>
  7. </view>
  8. <u-search input-align="left" @change="searchAddress" :show-action="false" height="70" placeholder="请输入位置信息"
  9. v-model="addressName"></u-search>
  10. <!-- <u-search input-align="left" :disabled="true" @click="searchAddress" :show-action="false" height="70"
  11. placeholder="请输入位置信息" v-model="addressName"></u-search> -->
  12. </view>
  13. <view class="header2">
  14. <view class="header2-tip Regular">
  15. 当前定位
  16. </view>
  17. <view class="header2-content">
  18. <view class="left Medium">
  19. <u-icon name="arrow-down"></u-icon>
  20. <view class="">{{address}}</view>
  21. <view class="" v-if="isposition">{{text}}</view>
  22. </view>
  23. <view class="reposition Regular" @click="reloadPosition">{{po_tips}}</view>
  24. </view>
  25. </view>
  26. <view class="position-list Regular">
  27. <view class="search">
  28. <view>最近搜索</view>
  29. <u-icon name="trash-fill" color="#AFB3BF" size="28" @click="delList"></u-icon>
  30. </view>
  31. <view class="search-list">
  32. <view v-for="(item,index) in list" class="search-list-item" @click="toHome(item)">
  33. {{item.name}}
  34. </view>
  35. </view>
  36. </view>
  37. <view>
  38. <view v-html="item.name" v-for="(item,index) in searchList" :key="index">
  39. <!-- {{item.name}} -->
  40. </view>
  41. </view>
  42. </view>
  43. </template>
  44. <script>
  45. import permision from "@/js_sdk/wa-permission/permission.js"
  46. import amap from '@/js_sdk/js-amap/amap-wx.130.js'
  47. import uniSegmentedControl from "@/components/uni-segmented-control/uni-segmented-control.vue"
  48. export default {
  49. components: {
  50. uniSegmentedControl
  51. },
  52. data() {
  53. return {
  54. searchList: [],
  55. nowRegion: {},
  56. addressName: "",
  57. isposition: false,
  58. isIos: true,
  59. address: "定位失败,请点击重新定位",
  60. city: '北京',
  61. list: [],
  62. position: '',
  63. addressObj: {},
  64. seconds: 3,
  65. po_tips: '重新定位',
  66. seach: {
  67. latitude: 39.92,
  68. longitude: 116.46,
  69. }
  70. }
  71. },
  72. onShow() {
  73. let that = this
  74. uni.getStorage({
  75. key: 'nowRegion',
  76. success: (res) => {
  77. console.log("nowRegion", res.data)
  78. that.nowRegion = res.data
  79. this.seach.longitude = res.data.longitude
  80. this.seach.latitude = res.data.latitude
  81. that.city = res.data.positionName.cityName
  82. // console.log("city", that.city)
  83. uni.removeStorage({
  84. key: 'nowRegion'
  85. })
  86. },
  87. })
  88. },
  89. onLoad(e) {debugger
  90. var p1 = [116.434027, 39.941037];
  91. var p2 = [116.461665, 39.941564];
  92. // 返回 p1 到 p2 间的地面距离,单位:米
  93. var dis = amap.GeometryUtil.distance(p1, p2);
  94. console.log("dis",dis)
  95. let that = this
  96. uni.getStorage({
  97. key: 'SearchList_key',
  98. success: function(res) {
  99. that.list = res.data
  100. }
  101. });
  102. this.getLocation()
  103. // 获取定位
  104. // this.utils.getLocation().then(function(res) {
  105. // debugger
  106. // console.log("定位信息",res)
  107. // this.city = res.address.city
  108. // this.address = res.address.poiName
  109. // console.log('city',this.city)
  110. // console.log('poiName',this.address)
  111. // if (res.errMsg != "getLocation:ok") {
  112. // // 定位权限未开启,引导设置
  113. // uni.showModal({
  114. // title: '温馨提示',
  115. // content: '您已拒绝定位,请开启',
  116. // confirmText: '去设置',
  117. // success(res) {
  118. // if (res.confirm) {
  119. // //打开授权设置
  120. // this.utils.openSetting()
  121. // }
  122. // }
  123. // })
  124. // }
  125. // })
  126. },
  127. methods: {
  128. // 删除最近搜索
  129. delList() {
  130. uni.setStorage({
  131. key: 'SearchList_key',
  132. data: []
  133. });
  134. },
  135. getLocation() {
  136. let that = this;
  137. uni.getLocation({
  138. type: 'gcj02',
  139. geocode: true,
  140. success: function(res) {
  141. // console.log('获取位置数据:', res);
  142. // console.log('当前位置的经度:' + res.longitude);
  143. // console.log('当前位置的纬度:' + res.latitude);
  144. //拼接当前定位回显地址
  145. // #ifdef APP-PLUS
  146. let _address = res.address
  147. // that.city = _address.city
  148. that.address = _address.province + _address.city + _address.district + _address.poiName
  149. console.log(that.address)
  150. // #endif
  151. }
  152. });
  153. },
  154. // 重新定位
  155. reloadPosition() {
  156. var that = this
  157. that.po_tips = '定位中...'
  158. let countdown = setInterval(() => {
  159. that.seconds--;
  160. uni.getLocation({
  161. type: 'gcj02',
  162. geocode: true,
  163. success: function(res) {
  164. console.log('当前位置的经度:' + res.longitude);
  165. console.log('当前位置的纬度:' + res.latitude);
  166. // #ifdef APP-PLUS
  167. let _address = res.address
  168. that.city = _address.city
  169. that.longitude = res.longitude
  170. that.latitude = res.latitude
  171. that.address = _address.province + _address.city + _address.district +
  172. _address.poiName
  173. // #endif
  174. }
  175. });
  176. if (that.seconds <= 0) {
  177. that.seconds = 3
  178. that.po_tips = '重新定位'
  179. clearInterval(countdown);
  180. }
  181. }, 1000);
  182. },
  183. openSetting() {
  184. // App跳转系统的设置界面
  185. // #ifdef APP-PLUS
  186. uni.getSystemInfo({
  187. success(res) {
  188. if (res.platform == 'ios') { //IOS
  189. plus.runtime.openURL("app-settings://");
  190. } else if (res.platform == 'android') { //安卓
  191. let main = plus.android.runtimeMainActivity();
  192. let Intent = plus.android.importClass("android.content.Intent");
  193. let mIntent = new Intent('android.settings.ACTION_SETTINGS');
  194. main.startActivity(mIntent);
  195. }
  196. }
  197. });
  198. // #endif
  199. },
  200. // 跳转ios权限界面
  201. judgeIosPermission: function(permisionID) {
  202. var result = permision.judgeIosPermission(permisionID)
  203. // console.log(result);
  204. var strStatus = (result) ? "已" : "未"
  205. uni.showModal({
  206. content: permisionID + '权限' + strStatus + "获得授权",
  207. showCancel: false
  208. });
  209. },
  210. // 跳转安卓权限界面
  211. async requestAndroidPermission(permisionID) {
  212. var result = await permision.requestAndroidPermission(permisionID)
  213. var strStatus
  214. if (result == 1) {
  215. strStatus = "已获得授权"
  216. } else if (result == 0) {
  217. strStatus = "未获得授权"
  218. } else {
  219. strStatus = "被永久拒绝权限"
  220. }
  221. uni.showModal({
  222. content: permisionID + strStatus,
  223. showCancel: false
  224. });
  225. },
  226. //设置搜索关键字颜色
  227. getStringColorReplace(val, val1) {
  228. for (let j = 0; j < val.length; j++) {
  229. var replaceString = '<view style="color:red' + ';">' + val1 + "</view>";
  230. val[j].name = val[j].name.replace(RegExp(val1, 'g'), replaceString);
  231. }
  232. },
  233. // 关键字搜索
  234. searchAddress(val) {
  235. console.log(1222)
  236. let parameters = {
  237. key: 'dd701d394d116b50268dc16470ddd615',
  238. keywords: val,
  239. region: this.city,
  240. page_size: 25
  241. }
  242. uni.request({
  243. url: 'https://restapi.amap.com/v5/place/text',
  244. data: parameters,
  245. success: (res) => {
  246. console.log(res.data);
  247. this.searchList = res.data.pois
  248. this.getStringColorReplace(res.data.pois, val)
  249. }
  250. });
  251. },
  252. // 搜索
  253. // searchAddress(val) {
  254. // // console.log(val)
  255. // let that = this;
  256. // console.log("search", that.seach)
  257. // uni.chooseLocation({
  258. // latitude: that.seach.latitude,
  259. // longitude: that.seach.longitude,
  260. // success: function(res) {
  261. // console.log(res)
  262. // // console.log('位置名称:' + res.name);
  263. // // console.log('详细地址:' + res.address);
  264. // // console.log('纬度:' + res.latitude);
  265. // // console.log('经度:' + res.longitude);
  266. // that.list.push(res)
  267. // this.position = res.name
  268. // uni.setStorage({
  269. // key: 'SearchList_key',
  270. // data: that.list
  271. // });
  272. // uni.setStorage({
  273. // key: 'setLocaltion',
  274. // data: res,
  275. // success() {
  276. // uni.switchTab({
  277. // url: "../home"
  278. // });
  279. // }
  280. // })
  281. // }
  282. // });
  283. // },
  284. // 切换城市
  285. loadCity() {
  286. uni.navigateTo({
  287. url: "../selectCity/selectCity"
  288. })
  289. },
  290. toHome(item) {
  291. console.log("跳转首页")
  292. uni.setStorage({
  293. key: 'setLocaltion',
  294. data: item,
  295. success() {
  296. uni.switchTab({
  297. url: "../home",
  298. success(e) {
  299. console.log(e)
  300. }
  301. });
  302. }
  303. })
  304. }
  305. }
  306. }
  307. </script>
  308. <style lang="scss" scoped>
  309. /deep/.u-content {
  310. padding-left: 170rpx;
  311. }
  312. .header {
  313. position: relative;
  314. display: flex;
  315. background: white;
  316. padding: 20rpx;
  317. .city {
  318. top: 0;
  319. bottom: 0;
  320. left: 50rpx;
  321. margin: auto;
  322. position: absolute;
  323. display: flex;
  324. align-items: center;
  325. .select-city {
  326. margin-right: 4rpx;
  327. }
  328. }
  329. }
  330. .header2 {
  331. background: white;
  332. border-radius: 0 0 20rpx 20rpx;
  333. padding: 0 20rpx 34rpx 20rpx;
  334. .header2-tip {
  335. font-size: 28rpx;
  336. color: #AFB3BF;
  337. }
  338. .header2-content {
  339. font-size: 32rpx;
  340. color: #333333;
  341. display: flex;
  342. justify-content: space-between;
  343. margin-top: 12rpx;
  344. .left {
  345. display: flex;
  346. }
  347. .reposition {
  348. font-size: 28rpx;
  349. color: #22C572;
  350. }
  351. }
  352. }
  353. .position-list {
  354. margin-top: 20rpx;
  355. background: white;
  356. border-radius: 20rpx 20rpx 0 0;
  357. padding: 10rpx;
  358. }
  359. .search {
  360. display: flex;
  361. justify-content: space-between;
  362. padding: 0 20rpx;
  363. }
  364. .search-list {
  365. padding: 20rpx;
  366. }
  367. .search-list-item {
  368. display: inline-block;
  369. background: #F5F6F9;
  370. border-radius: 32rpx;
  371. padding: 16rpx 24rpx;
  372. margin: 0 20rpx 20rpx 0;
  373. }
  374. </style>