position.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. uni.setStorage({
  128. key: 'setPosition',
  129. data: item,
  130. success: (res) => {
  131. uni.setStorage({
  132. key: 'setLocaltion',
  133. data: item,
  134. success: (res) => {
  135. uni.navigateBack({
  136. })
  137. },
  138. })
  139. }
  140. })
  141. },
  142. // 排序
  143. compare(property) {
  144. return function(a, b) {
  145. var value1 = a[property];
  146. var value2 = b[property];
  147. return value1 - value2;
  148. }
  149. },
  150. // 经纬度计算距离
  151. getDistance(lat1, lng1, lat2, lng2) {
  152. lat2 = parseFloat(lat2)
  153. lng2 = parseFloat(lng2)
  154. console.log(lat1, lng1, lat2, lng2)
  155. var radLat1 = lat1 * Math.PI / 180.0;
  156. var radLat2 = lat2 * Math.PI / 180.0;
  157. var a = radLat1 - radLat2;
  158. var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  159. var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
  160. Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
  161. s = s * 6378.137; // EARTH_RADIUS;
  162. s = Math.round(s * 10000) / 10000;
  163. console.log("计算结果", s, 'KM');
  164. return s
  165. },
  166. // 删除最近搜索
  167. delList() {
  168. uni.setStorage({
  169. key: 'SearchList_key',
  170. data: []
  171. });
  172. },
  173. getLocation() {
  174. let that = this;
  175. console.log(11111)
  176. uni.getLocation({
  177. type: 'gcj02',
  178. geocode: true,
  179. success: function(res) {
  180. console.log('获取位置数据:', res);
  181. console.log('当前位置的经度:' + res.longitude);
  182. console.log('当前位置的纬度:' + res.latitude);
  183. //拼接当前定位回显地址
  184. // #ifdef APP-PLUS
  185. that.location = res
  186. let _address = res.address
  187. that.city = _address.city
  188. that.seach.longitude = res.longitude
  189. that.seach.latitude = res.latitude
  190. that.address = _address.province + _address.city + _address.district + _address
  191. .poiName
  192. that.searchAddress(_address.poiName)
  193. that.addressName = _address.poiName
  194. console.log(that.address)
  195. // #endif
  196. },
  197. fail:function(req){
  198. console.log(req)
  199. }
  200. });
  201. },
  202. // 重新定位
  203. reloadPosition() {
  204. console.log(111111)
  205. var that = this
  206. that.po_tips = '定位中...'
  207. let countdown = setInterval(() => {
  208. that.seconds--;
  209. uni.getLocation({
  210. type: 'gcj02',
  211. geocode: true,
  212. success: function(res) {
  213. console.log('当前位置的经度:' + res.longitude);
  214. console.log('当前位置的纬度:' + res.latitude);
  215. // #ifdef APP-PLUS
  216. that.location = res
  217. let _address = res.address
  218. that.city = _address.city
  219. that.seach.longitude = res.longitude
  220. that.seach.latitude = res.latitude
  221. that.address = _address.province + _address.city + _address
  222. .district +
  223. _address.poiName
  224. that.searchAddress(_address.poiName)
  225. that.addressName = _address.poiName
  226. // #endif
  227. }
  228. });
  229. if (that.seconds <= 0) {
  230. that.seconds = 3
  231. that.po_tips = '重新定位'
  232. clearInterval(countdown);
  233. }
  234. }, 1000);
  235. },
  236. openSetting() {
  237. // App跳转系统的设置界面
  238. // #ifdef APP-PLUS
  239. uni.getSystemInfo({
  240. success(res) {
  241. if (res.platform == 'ios') { //IOS
  242. plus.runtime.openURL("app-settings://");
  243. } else if (res.platform == 'android') { //安卓
  244. let main = plus.android.runtimeMainActivity();
  245. let Intent = plus.android.importClass("android.content.Intent");
  246. let mIntent = new Intent('android.settings.ACTION_SETTINGS');
  247. main.startActivity(mIntent);
  248. }
  249. }
  250. });
  251. // #endif
  252. },
  253. // 跳转ios权限界面
  254. judgeIosPermission: function(permisionID) {
  255. var result = permision.judgeIosPermission(permisionID)
  256. // console.log(result);
  257. var strStatus = (result) ? "已" : "未"
  258. uni.showModal({
  259. content: permisionID + '权限' + strStatus + "获得授权",
  260. showCancel: false
  261. });
  262. },
  263. // 跳转安卓权限界面
  264. async requestAndroidPermission(permisionID) {
  265. var result = await permision.requestAndroidPermission(permisionID)
  266. var strStatus
  267. if (result == 1) {
  268. strStatus = "已获得授权"
  269. } else if (result == 0) {
  270. strStatus = "未获得授权"
  271. } else {
  272. strStatus = "被永久拒绝权限"
  273. }
  274. uni.showModal({
  275. content: permisionID + strStatus,
  276. showCancel: false
  277. });
  278. },
  279. //设置搜索关键字颜色
  280. getStringColorReplace(val, val1) {
  281. let _lat1 = this.seach.latitude
  282. let _lng1 = this.seach.longitude
  283. for (let j = 0; j < val.length; j++) {
  284. var replaceString = '<view style="color:#22C572' + ';">' + val1 + "</view>";
  285. val[j].newName = val[j].name.replace(RegExp(val1, 'g'), replaceString);
  286. let _lat2 = val[j].location.split(',')[1];
  287. let _lng2 = val[j].location.split(',')[0];
  288. val[j].distance = this.getDistance(_lat1, _lng1, _lat2, _lng2)
  289. }
  290. //排序
  291. this.searchList.sort(this.compare("distance"))
  292. },
  293. // 关键字搜索
  294. searchAddress(val) {
  295. console.log(1222)
  296. let parameters = {
  297. key: 'dd701d394d116b50268dc16470ddd615',
  298. keywords: val,
  299. region: this.city,
  300. page_size: 25
  301. }
  302. uni.request({
  303. url: 'https://restapi.amap.com/v5/place/text',
  304. data: parameters,
  305. success: (res) => {
  306. console.log("searchAddress",res.data);
  307. this.searchList = res.data.pois
  308. this.getStringColorReplace(res.data.pois, val)
  309. }
  310. });
  311. },
  312. // 切换城市
  313. loadCity() {
  314. uni.navigateTo({
  315. url: "../selectCity/selectCity"
  316. })
  317. },
  318. selectPosition(){
  319. // uni.setStorage({
  320. // key: 'setPosition',
  321. // data: this.location,
  322. // success: (res) => {
  323. // uni.setStorage({
  324. // key: 'setLocaltion',
  325. // data: this.location,
  326. // success: (res) => {
  327. // uni.navigateBack({
  328. // })
  329. // },
  330. // })
  331. // }
  332. // })
  333. },
  334. toHome(item) {
  335. uni.setStorage({
  336. key: 'setPosition',
  337. data: item,
  338. success: (res) => {
  339. uni.setStorage({
  340. key: 'setLocaltion',
  341. data: item,
  342. success: (res) => {
  343. uni.navigateBack({
  344. })
  345. },
  346. })
  347. }
  348. })
  349. }
  350. }
  351. }
  352. </script>
  353. <style lang="scss" scoped>
  354. /deep/.u-content {
  355. padding-left: 170rpx;
  356. }
  357. .header {
  358. position: relative;
  359. display: flex;
  360. background: white;
  361. padding: 20rpx;
  362. .city {
  363. top: 0;
  364. bottom: 0;
  365. left: 50rpx;
  366. margin: auto;
  367. position: absolute;
  368. display: flex;
  369. align-items: center;
  370. .select-city {
  371. margin-right: 4rpx;
  372. }
  373. }
  374. }
  375. .header2 {
  376. background: white;
  377. border-radius: 0 0 20rpx 20rpx;
  378. padding: 0 20rpx 34rpx 20rpx;
  379. .header2-tip {
  380. font-size: 28rpx;
  381. color: #AFB3BF;
  382. }
  383. .header2-content {
  384. font-size: 32rpx;
  385. color: #333333;
  386. display: flex;
  387. justify-content: space-between;
  388. margin-top: 12rpx;
  389. .left {
  390. display: flex;
  391. }
  392. .reposition {
  393. font-size: 28rpx;
  394. color: #22C572;
  395. }
  396. }
  397. }
  398. .position-list {
  399. margin-top: 20rpx;
  400. background: white;
  401. border-radius: 20rpx 20rpx 0 0;
  402. padding: 10rpx;
  403. }
  404. .search {
  405. display: flex;
  406. justify-content: space-between;
  407. padding: 0 20rpx;
  408. }
  409. .search-list {
  410. padding: 20rpx;
  411. }
  412. .search-list-item {
  413. display: inline-block;
  414. background: #F5F6F9;
  415. border-radius: 32rpx;
  416. padding: 16rpx 24rpx;
  417. margin: 0 20rpx 20rpx 0;
  418. }
  419. .search-content {
  420. border-radius: 20rpx 20rpx 0 0;
  421. margin-top: 20rpx;
  422. padding: 10rpx 20rpx;
  423. background: white;
  424. .search-contnet-item {
  425. border-bottom: 1px solid #EEEEEE;
  426. padding-bottom: 10rpx;
  427. margin-top: 20rpx;
  428. .top {
  429. display: flex;
  430. justify-content: space-between;
  431. .content {
  432. font-size: 28rpx;
  433. font-weight: 400;
  434. }
  435. .distance {
  436. font-size: 24rpx;
  437. font-weight: 400;
  438. color: #AFB3BF;
  439. }
  440. }
  441. .bottom {
  442. margin-top: 6rpx;
  443. font-size: 24rpx;
  444. font-weight: 400;
  445. color: #AFB3BF;
  446. }
  447. }
  448. }
  449. </style>