position.vue 11 KB

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