position.vue 11 KB

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