position.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. </view>
  11. <view class="header2">
  12. <view class="header2-tip Regular">
  13. 当前定位
  14. </view>
  15. <view class="header2-content">
  16. <view class="left Medium" @click="selectPosition">
  17. <u-icon name="arrow-down"></u-icon>
  18. <view class="">{{address}}</view>
  19. <view class="" v-if="isposition">{{text}}</view>
  20. </view>
  21. <view class="reposition Regular" @click="reloadPosition">{{po_tips}}</view>
  22. </view>
  23. </view>
  24. <view class="position-list Regular" v-if="addressName==''">
  25. <view class="search">
  26. <view>最近搜索</view>
  27. <u-icon name="trash-fill" color="#AFB3BF" size="28" @click="delList"></u-icon>
  28. </view>
  29. <view class="search-list">
  30. <view v-for="(item,index) in list" class="search-list-item" @click="toHome(item)">
  31. {{item.name}}
  32. </view>
  33. </view>
  34. </view>
  35. <view class="search-content" v-if="addressName!=''">
  36. <view v-for="(item,index) in searchList" :key="index" class="search-contnet-item"
  37. @click="addressClick(item)">
  38. <view class="top">
  39. <view v-html="item.newName" class="content"></view>
  40. <view class="distance" v-if="address!='定位失败,请点击重新定位'">{{item.distance}}千米</view>
  41. </view>
  42. <view class="bottom">
  43. {{item.adname}}{{item.address}}
  44. </view>
  45. </view>
  46. </view>
  47. </view>
  48. </template>
  49. <script>
  50. import permision from "@/js_sdk/wa-permission/permission.js"
  51. import amap from '@/js_sdk/js-amap/amap-wx.130.js'
  52. import uniSegmentedControl from "@/components/uni-segmented-control/uni-segmented-control.vue"
  53. export default {
  54. components: {
  55. uniSegmentedControl
  56. },
  57. data() {
  58. return {
  59. searchList: [],
  60. nowRegion: {},
  61. addressName: "",
  62. isposition: false,
  63. isIos: true,
  64. address: "定位失败,请点击重新定位",
  65. city: '北京',
  66. list: [],
  67. position: '',
  68. addressObj: {},
  69. seconds: 3,
  70. po_tips: '重新定位',
  71. seach: {
  72. latitude: 39.92,
  73. longitude: 116.46,
  74. },
  75. positionType: {
  76. type: ""
  77. },
  78. location:{}
  79. }
  80. },
  81. onShow() {
  82. let that = this
  83. uni.getStorage({
  84. key: 'nowRegion',
  85. success: (res) => {
  86. console.log("nowRegion", res.data)
  87. that.nowRegion = res.data
  88. this.seach.longitude = res.data.longitude
  89. this.seach.latitude = res.data.latitude
  90. that.city = res.data.positionName.cityName
  91. console.log("that.city", that.city)
  92. // console.log("city", that.city)
  93. // uni.removeStorage({
  94. // key: 'nowRegion'
  95. // })
  96. },
  97. })
  98. uni.getStorage({
  99. key: 'setPositionType',
  100. success: (res) => {
  101. console.log("positionType", res.data)
  102. this.positionType = res.data
  103. },
  104. })
  105. },
  106. onLoad(e) {
  107. // var p1 = [116.434027, 39.941037];
  108. // var p2 = [116.461665, 39.941564];
  109. let that = this
  110. uni.getStorage({
  111. key: 'SearchList_key',
  112. success: function(res) {
  113. that.list = res.data
  114. }
  115. });
  116. this.getLocation()
  117. },
  118. methods: {
  119. addressClick(item) {
  120. if(item){
  121. this.list.push(item)
  122. uni.setStorage({
  123. key: 'SearchList_key',
  124. data: this.list
  125. });
  126. }
  127. if (this.positionType.type == 1) {
  128. uni.setStorage({
  129. key: 'setPosition',
  130. data: item,
  131. success: (res) => {
  132. uni.navigateBack({
  133. })
  134. }
  135. })
  136. } else {
  137. uni.setStorage({
  138. key: 'setLocaltion',
  139. data: item,
  140. success: (res) => {
  141. uni.switchTab({
  142. url: '../home'
  143. })
  144. },
  145. })
  146. }
  147. },
  148. // 排序
  149. compare(property) {
  150. return function(a, b) {
  151. var value1 = a[property];
  152. var value2 = b[property];
  153. return value1 - value2;
  154. }
  155. },
  156. // 经纬度计算距离
  157. getDistance(lat1, lng1, lat2, lng2) {
  158. lat2 = parseFloat(lat2)
  159. lng2 = parseFloat(lng2)
  160. console.log(lat1, lng1, lat2, lng2)
  161. var radLat1 = lat1 * Math.PI / 180.0;
  162. var radLat2 = lat2 * Math.PI / 180.0;
  163. var a = radLat1 - radLat2;
  164. var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  165. var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
  166. Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  167. s = s * 6378.137; // EARTH_RADIUS;
  168. s = Math.round(s * 10000) / 10000;
  169. console.log("计算结果", s, 'KM');
  170. return s
  171. },
  172. // 删除最近搜索
  173. delList() {
  174. uni.setStorage({
  175. key: 'SearchList_key',
  176. data: []
  177. });
  178. },
  179. getLocation() {
  180. let that = this;
  181. console.log(11111)
  182. uni.getLocation({
  183. type: 'gcj02',
  184. geocode: true,
  185. success: function(res) {
  186. console.log('获取位置数据:', res);
  187. console.log('当前位置的经度:' + res.longitude);
  188. console.log('当前位置的纬度:' + res.latitude);
  189. //拼接当前定位回显地址
  190. // #ifdef APP-PLUS
  191. that.location = res
  192. let _address = res.address
  193. // that.city = _address.city
  194. that.seach.longitude = res.longitude
  195. that.seach.latitude = res.latitude
  196. that.address = _address.province + _address.city + _address.district + _address
  197. .poiName
  198. that.searchAddress(_address.poiName)
  199. that.addressName = _address.poiName
  200. console.log(that.address)
  201. // #endif
  202. },
  203. fail:function(req){
  204. console.log(req)
  205. }
  206. });
  207. },
  208. // 重新定位
  209. reloadPosition() {
  210. console.log(111111)
  211. var that = this
  212. that.po_tips = '定位中...'
  213. let countdown = setInterval(() => {
  214. that.seconds--;
  215. uni.getLocation({
  216. type: 'gcj02',
  217. geocode: true,
  218. success: function(res) {
  219. console.log('当前位置的经度:' + res.longitude);
  220. console.log('当前位置的纬度:' + res.latitude);
  221. // #ifdef APP-PLUS
  222. that.location = res
  223. let _address = res.address
  224. that.city = _address.city
  225. that.seach.longitude = res.longitude
  226. that.seach.latitude = res.latitude
  227. that.address = _address.province + _address.city + _address
  228. .district +
  229. _address.poiName
  230. that.searchAddress(_address.poiName)
  231. that.addressName = _address.poiName
  232. // #endif
  233. }
  234. });
  235. if (that.seconds <= 0) {
  236. that.seconds = 3
  237. that.po_tips = '重新定位'
  238. clearInterval(countdown);
  239. }
  240. }, 1000);
  241. },
  242. openSetting() {
  243. // App跳转系统的设置界面
  244. // #ifdef APP-PLUS
  245. uni.getSystemInfo({
  246. success(res) {
  247. if (res.platform == 'ios') { //IOS
  248. plus.runtime.openURL("app-settings://");
  249. } else if (res.platform == 'android') { //安卓
  250. let main = plus.android.runtimeMainActivity();
  251. let Intent = plus.android.importClass("android.content.Intent");
  252. let mIntent = new Intent('android.settings.ACTION_SETTINGS');
  253. main.startActivity(mIntent);
  254. }
  255. }
  256. });
  257. // #endif
  258. },
  259. // 跳转ios权限界面
  260. judgeIosPermission: function(permisionID) {
  261. var result = permision.judgeIosPermission(permisionID)
  262. // console.log(result);
  263. var strStatus = (result) ? "已" : "未"
  264. uni.showModal({
  265. content: permisionID + '权限' + strStatus + "获得授权",
  266. showCancel: false
  267. });
  268. },
  269. // 跳转安卓权限界面
  270. async requestAndroidPermission(permisionID) {
  271. var result = await permision.requestAndroidPermission(permisionID)
  272. var strStatus
  273. if (result == 1) {
  274. strStatus = "已获得授权"
  275. } else if (result == 0) {
  276. strStatus = "未获得授权"
  277. } else {
  278. strStatus = "被永久拒绝权限"
  279. }
  280. uni.showModal({
  281. content: permisionID + strStatus,
  282. showCancel: false
  283. });
  284. },
  285. //设置搜索关键字颜色
  286. getStringColorReplace(val, val1) {
  287. let _lat1 = this.seach.latitude
  288. let _lng1 = this.seach.longitude
  289. for (let j = 0; j < val.length; j++) {
  290. var replaceString = '<view style="color:#22C572' + ';">' + val1 + "</view>";
  291. val[j].newName = val[j].name.replace(RegExp(val1, 'g'), replaceString);
  292. let _lat2 = val[j].location.split(',')[1];
  293. let _lng2 = val[j].location.split(',')[0];
  294. val[j].distance = this.getDistance(_lat1, _lng1, _lat2, _lng2)
  295. }
  296. //排序
  297. this.searchList.sort(this.compare("distance"))
  298. },
  299. // 关键字搜索
  300. searchAddress(val) {
  301. console.log(1222)
  302. let parameters = {
  303. key: 'dd701d394d116b50268dc16470ddd615',
  304. keywords: val,
  305. region: this.city,
  306. page_size: 25
  307. }
  308. uni.request({
  309. url: 'https://restapi.amap.com/v5/place/text',
  310. data: parameters,
  311. success: (res) => {
  312. console.log("searchAddress",res.data);
  313. this.searchList = res.data.pois
  314. this.getStringColorReplace(res.data.pois, val)
  315. }
  316. });
  317. },
  318. // 切换城市
  319. loadCity() {
  320. uni.navigateTo({
  321. url: "../selectCity/selectCity"
  322. })
  323. },
  324. selectPosition(){
  325. // uni.setStorage({
  326. // key: 'setPosition',
  327. // data: this.location,
  328. // success: (res) => {
  329. // uni.setStorage({
  330. // key: 'setLocaltion',
  331. // data: this.location,
  332. // success: (res) => {
  333. // uni.navigateBack({
  334. // })
  335. // },
  336. // })
  337. // }
  338. // })
  339. },
  340. toHome(item) {
  341. uni.setStorage({
  342. key: 'setPosition',
  343. data: item,
  344. success: (res) => {
  345. uni.setStorage({
  346. key: 'setLocaltion',
  347. data: item,
  348. success: (res) => {
  349. uni.navigateBack({
  350. })
  351. },
  352. })
  353. }
  354. })
  355. }
  356. }
  357. }
  358. </script>
  359. <style lang="scss" scoped>
  360. /deep/.u-content {
  361. padding-left: 170rpx;
  362. }
  363. .header {
  364. position: relative;
  365. display: flex;
  366. background: white;
  367. padding: 20rpx;
  368. .city {
  369. top: 0;
  370. bottom: 0;
  371. left: 50rpx;
  372. margin: auto;
  373. position: absolute;
  374. display: flex;
  375. align-items: center;
  376. .select-city {
  377. margin-right: 4rpx;
  378. }
  379. }
  380. }
  381. .header2 {
  382. background: white;
  383. border-radius: 0 0 20rpx 20rpx;
  384. padding: 0 20rpx 34rpx 20rpx;
  385. .header2-tip {
  386. font-size: 28rpx;
  387. color: #AFB3BF;
  388. }
  389. .header2-content {
  390. font-size: 32rpx;
  391. color: #333333;
  392. display: flex;
  393. justify-content: space-between;
  394. margin-top: 12rpx;
  395. .left {
  396. display: flex;
  397. }
  398. .reposition {
  399. font-size: 28rpx;
  400. color: #22C572;
  401. }
  402. }
  403. }
  404. .position-list {
  405. margin-top: 20rpx;
  406. background: white;
  407. border-radius: 20rpx 20rpx 0 0;
  408. padding: 10rpx;
  409. }
  410. .search {
  411. display: flex;
  412. justify-content: space-between;
  413. padding: 0 20rpx;
  414. }
  415. .search-list {
  416. padding: 20rpx;
  417. }
  418. .search-list-item {
  419. display: inline-block;
  420. background: #F5F6F9;
  421. border-radius: 32rpx;
  422. padding: 16rpx 24rpx;
  423. margin: 0 20rpx 20rpx 0;
  424. }
  425. .search-content {
  426. border-radius: 20rpx 20rpx 0 0;
  427. margin-top: 20rpx;
  428. padding: 10rpx 20rpx;
  429. background: white;
  430. .search-contnet-item {
  431. border-bottom: 1px solid #EEEEEE;
  432. padding-bottom: 10rpx;
  433. margin-top: 20rpx;
  434. .top {
  435. display: flex;
  436. justify-content: space-between;
  437. .content {
  438. font-size: 28rpx;
  439. font-weight: 400;
  440. }
  441. .distance {
  442. font-size: 24rpx;
  443. font-weight: 400;
  444. color: #AFB3BF;
  445. }
  446. }
  447. .bottom {
  448. margin-top: 6rpx;
  449. font-size: 24rpx;
  450. font-weight: 400;
  451. color: #AFB3BF;
  452. }
  453. }
  454. }
  455. </style>