position.vue 11 KB

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