position.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. 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. });
  199. },
  200. // 重新定位
  201. reloadPosition() {
  202. var that = this
  203. that.po_tips = '定位中...'
  204. let countdown = setInterval(() => {
  205. that.seconds--;
  206. uni.getLocation({
  207. type: 'gcj02',
  208. geocode: true,
  209. success: function(res) {
  210. console.log('当前位置的经度:' + res.longitude);
  211. console.log('当前位置的纬度:' + res.latitude);
  212. // #ifdef APP-PLUS
  213. let _address = res.address
  214. that.city = _address.city
  215. that.seach.longitude = res.longitude
  216. that.seach.latitude = res.latitude
  217. that.address = _address.province + _address.city + _address
  218. .district +
  219. _address.poiName
  220. // #endif
  221. }
  222. });
  223. if (that.seconds <= 0) {
  224. that.seconds = 3
  225. that.po_tips = '重新定位'
  226. clearInterval(countdown);
  227. }
  228. }, 1000);
  229. },
  230. openSetting() {
  231. // App跳转系统的设置界面
  232. // #ifdef APP-PLUS
  233. uni.getSystemInfo({
  234. success(res) {
  235. if (res.platform == 'ios') { //IOS
  236. plus.runtime.openURL("app-settings://");
  237. } else if (res.platform == 'android') { //安卓
  238. let main = plus.android.runtimeMainActivity();
  239. let Intent = plus.android.importClass("android.content.Intent");
  240. let mIntent = new Intent('android.settings.ACTION_SETTINGS');
  241. main.startActivity(mIntent);
  242. }
  243. }
  244. });
  245. // #endif
  246. },
  247. // 跳转ios权限界面
  248. judgeIosPermission: function(permisionID) {
  249. var result = permision.judgeIosPermission(permisionID)
  250. // console.log(result);
  251. var strStatus = (result) ? "已" : "未"
  252. uni.showModal({
  253. content: permisionID + '权限' + strStatus + "获得授权",
  254. showCancel: false
  255. });
  256. },
  257. // 跳转安卓权限界面
  258. async requestAndroidPermission(permisionID) {
  259. var result = await permision.requestAndroidPermission(permisionID)
  260. var strStatus
  261. if (result == 1) {
  262. strStatus = "已获得授权"
  263. } else if (result == 0) {
  264. strStatus = "未获得授权"
  265. } else {
  266. strStatus = "被永久拒绝权限"
  267. }
  268. uni.showModal({
  269. content: permisionID + strStatus,
  270. showCancel: false
  271. });
  272. },
  273. //设置搜索关键字颜色
  274. getStringColorReplace(val, val1) {
  275. let _lat1 = this.seach.latitude
  276. let _lng1 = this.seach.longitude
  277. for (let j = 0; j < val.length; j++) {
  278. var replaceString = '<view style="color:#22C572' + ';">' + val1 + "</view>";
  279. val[j].newName = val[j].name.replace(RegExp(val1, 'g'), replaceString);
  280. let _lat2 = val[j].location.split(',')[1];
  281. let _lng2 = val[j].location.split(',')[0];
  282. val[j].distance = this.getDistance(_lat1, _lng1, _lat2, _lng2)
  283. }
  284. //排序
  285. this.searchList.sort(this.compare("distance"))
  286. },
  287. // 关键字搜索
  288. searchAddress(val) {
  289. console.log(1222)
  290. let parameters = {
  291. key: 'dd701d394d116b50268dc16470ddd615',
  292. keywords: val,
  293. region: this.city,
  294. page_size: 25
  295. }
  296. uni.request({
  297. url: 'https://restapi.amap.com/v5/place/text',
  298. data: parameters,
  299. success: (res) => {
  300. console.log(res.data);
  301. this.searchList = res.data.pois
  302. this.getStringColorReplace(res.data.pois, val)
  303. }
  304. });
  305. },
  306. // 切换城市
  307. loadCity() {
  308. uni.navigateTo({
  309. url: "../selectCity/selectCity"
  310. })
  311. },
  312. toHome(item) {
  313. if (this.positionType.type == 1) {
  314. uni.setStorage({
  315. key: 'setPosition',
  316. data: item,
  317. success: (res) => {
  318. uni.navigateTo({
  319. url: '../localtion/coordinate'
  320. })
  321. }
  322. })
  323. } else {
  324. uni.setStorage({
  325. key: 'setLocaltion',
  326. data: item,
  327. success: (res) => {
  328. uni.switchTab({
  329. url: '../home'
  330. })
  331. },
  332. })
  333. }
  334. }
  335. }
  336. }
  337. </script>
  338. <style lang="scss" scoped>
  339. /deep/.u-content {
  340. padding-left: 170rpx;
  341. }
  342. .header {
  343. position: relative;
  344. display: flex;
  345. background: white;
  346. padding: 20rpx;
  347. .city {
  348. top: 0;
  349. bottom: 0;
  350. left: 50rpx;
  351. margin: auto;
  352. position: absolute;
  353. display: flex;
  354. align-items: center;
  355. .select-city {
  356. margin-right: 4rpx;
  357. }
  358. }
  359. }
  360. .header2 {
  361. background: white;
  362. border-radius: 0 0 20rpx 20rpx;
  363. padding: 0 20rpx 34rpx 20rpx;
  364. .header2-tip {
  365. font-size: 28rpx;
  366. color: #AFB3BF;
  367. }
  368. .header2-content {
  369. font-size: 32rpx;
  370. color: #333333;
  371. display: flex;
  372. justify-content: space-between;
  373. margin-top: 12rpx;
  374. .left {
  375. display: flex;
  376. }
  377. .reposition {
  378. font-size: 28rpx;
  379. color: #22C572;
  380. }
  381. }
  382. }
  383. .position-list {
  384. margin-top: 20rpx;
  385. background: white;
  386. border-radius: 20rpx 20rpx 0 0;
  387. padding: 10rpx;
  388. }
  389. .search {
  390. display: flex;
  391. justify-content: space-between;
  392. padding: 0 20rpx;
  393. }
  394. .search-list {
  395. padding: 20rpx;
  396. }
  397. .search-list-item {
  398. display: inline-block;
  399. background: #F5F6F9;
  400. border-radius: 32rpx;
  401. padding: 16rpx 24rpx;
  402. margin: 0 20rpx 20rpx 0;
  403. }
  404. .search-content {
  405. border-radius: 20rpx 20rpx 0 0;
  406. margin-top: 20rpx;
  407. padding: 10rpx 20rpx;
  408. background: white;
  409. .search-contnet-item {
  410. border-bottom: 1px solid #EEEEEE;
  411. padding-bottom: 10rpx;
  412. margin-top: 20rpx;
  413. .top {
  414. display: flex;
  415. justify-content: space-between;
  416. .content {
  417. font-size: 28rpx;
  418. font-weight: 400;
  419. }
  420. .distance {
  421. font-size: 24rpx;
  422. font-weight: 400;
  423. color: #AFB3BF;
  424. }
  425. }
  426. .bottom {
  427. margin-top: 6rpx;
  428. font-size: 24rpx;
  429. font-weight: 400;
  430. color: #AFB3BF;
  431. }
  432. }
  433. }
  434. </style>