position.vue 10.0 KB

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