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