map.nvue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <template>
  2. <view class="content">
  3. <map id="map" ref="map" :show-location="true" :style="{width:'750rpx',height:phoneHeight+'px'}"
  4. :latitude="latitude" :longitude="longitude" :markers="covers" :polyline="polylines">
  5. </map>
  6. <view class="fixed-bottom">
  7. <u-cell :title="date" isLink :border="false" @click="handleSelectDate"></u-cell>
  8. <u-button style="margin-left: 20rpx;" text="播放" type="primary" @click="handlePlayWalkTrack" size="small">
  9. </u-button>
  10. </view>
  11. <u-loading-page loading-text="加载中..." bgColor="transparent" :loading="loading" font-size="16"></u-loading-page>
  12. <u-calendar :monthNum="monthNum" :defaultDate="date" :minDate="minDate" :maxDate="maxDate"
  13. :show="calendarVisible" mode="single" @confirm="handleConfirmSelected" @close="handleCalendarClose">
  14. </u-calendar>
  15. </view>
  16. </template>
  17. <script>
  18. import * as trackService from '@/service/track.js'
  19. import {
  20. mapState,
  21. mapMutations,
  22. mapGetters
  23. } from 'vuex'
  24. import {
  25. forIn,
  26. takeRight,
  27. keys,
  28. map,
  29. first,
  30. flatten,
  31. uniqBy
  32. } from 'lodash-es';
  33. import dayjs from 'dayjs'
  34. const markerPath = '/static/person.png';
  35. export default {
  36. data() {
  37. return {
  38. show: false,
  39. isUpdateLocation: false,
  40. title: 'Hello',
  41. list: [{
  42. text: '点赞',
  43. color: 'blue',
  44. fontSize: 28
  45. }, {
  46. text: '分享'
  47. }, {
  48. text: '评论'
  49. }],
  50. show: true,
  51. id: 0, // 使用 marker点击事件 需要填写id
  52. title: 'map',
  53. latitude: 39.909,
  54. longitude: 116.39742,
  55. covers: [{
  56. id: 1,
  57. latitude: 39.909,
  58. longitude: 116.39742,
  59. iconPath: markerPath,
  60. width: 20,
  61. height: 20,
  62. }],
  63. // {
  64. // points: [{
  65. // latitude: 39.909,
  66. // longitude: 116.39742
  67. // },
  68. // {
  69. // latitude: 39.9,
  70. // longitude: 116.39
  71. // },
  72. // {
  73. // latitude: 39.88,
  74. // longitude: 116.34
  75. // },
  76. // {
  77. // latitude: 39.83,
  78. // longitude: 116.31
  79. // },
  80. // {
  81. // latitude: 39.8,
  82. // longitude: 116.3
  83. // },
  84. // ],
  85. // }
  86. logs: [],
  87. polylines: [],
  88. allLocationDatasource: {},
  89. phoneHeight: 600,
  90. loading: false,
  91. userinfo: {},
  92. page: 1,
  93. pageSize: 10,
  94. total: 0,
  95. date: dayjs().format('YYYY-MM-DD'),
  96. calendarVisible: false,
  97. minDate: dayjs().subtract(5, 'months').format('YYYY-MM-DD'),
  98. maxDate: dayjs().add(1, 'days').format('YYYY-MM-DD'),
  99. monthNum: 12,
  100. }
  101. },
  102. onLoad() {
  103. const that = this;
  104. uni.getSystemInfo({
  105. //异步获取。
  106. success(res) {
  107. that.phoneHeight = res.windowHeight; //窗口高度
  108. console.log(that.phoneHeight)
  109. },
  110. })
  111. },
  112. onReady() {
  113. console.log(this.$refs.map, 'on ready')
  114. this._mapContext = uni.createMapContext('map', this);
  115. this.setCenterPosition()
  116. },
  117. computed: {
  118. ...mapGetters(['userUID']),
  119. currentLogs() {
  120. return takeRight(this.logs, 5)
  121. },
  122. },
  123. mounted() {
  124. this.init();
  125. },
  126. methods: {
  127. init() {
  128. this.getTrackList();
  129. },
  130. getTrackList() {
  131. this.loading = true;
  132. const payload = {
  133. page: this.page,
  134. page_item: this.pageSize,
  135. uid: this.userUID,
  136. create_time: this.date
  137. }
  138. console.warn(payload,'get track list payload')
  139. trackService.fetchTrackListWithDate(payload).then(res => {
  140. console.warn(res, '------------ track list')
  141. this.total = res.total_item;
  142. this.allLocationDatasource = first(res.page_list);
  143. this.updatePolylines();
  144. }).finally(() => {
  145. this.loading = false;
  146. })
  147. },
  148. updatePolylines() {
  149. const walkList = this.allLocationDatasource['walk_list'] || [];
  150. const groupWalkDataList = uniqBy(flatten(walkList), item => item.create_time.trim()).sort((a, b) => +dayjs(
  151. a.create_time) - (+dayjs(b.create_time)))
  152. this.polylines = map([groupWalkDataList], (singleWalkList, index) => {
  153. return {
  154. points: map(singleWalkList, item => {
  155. return {
  156. latitude: item.lat,
  157. longitude: item.lng
  158. }
  159. }),
  160. color: '#44b449',
  161. arrowLine: true,
  162. }
  163. })
  164. const startData = first(first(walkList));
  165. const startLocation = {
  166. latitude: startData.lat,
  167. longitude: startData.lng
  168. }
  169. this._mapContext.moveToLocation(startLocation);
  170. },
  171. setCenterPosition() {
  172. uni.getLocation({
  173. type: 'gcj02',
  174. altitude: true,
  175. success: (res) => {
  176. console.warn(res, 'get location')
  177. console.log('当前位置的经度1:' + res.longitude);
  178. console.log('当前位置的纬度1:' + res.latitude);
  179. this.logs.push(`center-position:当前位置的经度${res.longitude};当前位置的纬度${res.latitude}`)
  180. this.latitude = res.latitude;
  181. this.longitude = res.longitude;
  182. this._mapContext.moveToLocation({
  183. latitude: res.latitude,
  184. longitude: res.longitude
  185. })
  186. this.covers = [{
  187. id: 1,
  188. latitude: res.latitude,
  189. longitude: res.longitude,
  190. iconPath: markerPath,
  191. width: 40,
  192. height: 40,
  193. }]
  194. },
  195. fail(err) {
  196. console.warn(err, 'get location error')
  197. }
  198. });
  199. },
  200. startGetLocation() {
  201. this.isUpdateLocation = true
  202. this.setCenterPosition()
  203. },
  204. endGetLocation() {
  205. this.isUpdateLocation = false
  206. },
  207. getLocation() {
  208. return new Promise((resolve, reject) => {
  209. console.log('getlocation start', this.isUpdateLocation)
  210. if (!this.isUpdateLocation) {
  211. console.log('getlocation start 1 close')
  212. resolve(true)
  213. return
  214. }
  215. console.log('getlocation start 2 exec')
  216. uni.getLocation({
  217. type: 'gcj02',
  218. success: (res) => {
  219. console.log('当前位置的经度:' + res.longitude);
  220. console.log('当前位置的纬度:' + res.latitude);
  221. this.logs.push(`记录中:当前位置的经度${res.longitude};当前位置的纬度${res.latitude}`)
  222. resolve(false)
  223. },
  224. fail(err) {
  225. console.warn(err, 'get location error')
  226. }
  227. });
  228. })
  229. },
  230. handleTransformHistory() {
  231. if (!this.polylines.length) {
  232. return
  233. }
  234. const startData = first(this.polylines[0].points)
  235. this._mapContext.moveToLocation({
  236. latitude: startData.latitude,
  237. longitude: startData.longitude
  238. })
  239. this.execMoveQueueList(map(this.polylines, item => item.points))
  240. },
  241. execMoveQueueList(queueList = []) {
  242. const markerID = this.covers[0] && this.covers[0].id;
  243. let index = 0;
  244. const that = this;
  245. const exec = function() {
  246. const linePointDataList = queueList[index] || [];
  247. const path =
  248. that._mapContext.moveAlong({
  249. markerId: markerID,
  250. path: linePointDataList,
  251. duration: 30000,
  252. autoRotation: false,
  253. success() {
  254. if (index < queueList.length - 1) {
  255. index++;
  256. exec();
  257. }
  258. },
  259. fail(err) {
  260. console.error(err)
  261. },
  262. complete() {
  263. console.log('move complete ----------------------')
  264. }
  265. })
  266. }
  267. exec();
  268. },
  269. handlePlayWalkTrack() {
  270. this.handleTransformHistory();
  271. },
  272. handleClearStorage() {
  273. console.log('clear storage')
  274. uni.clearStorage()
  275. },
  276. handleSelectDate() {
  277. this.calendarVisible = true;
  278. },
  279. handleConfirmSelected(dateList) {
  280. this.date = first(dateList) || dayjs().format('YYYY-MM-DD');
  281. this.handleCalendarClose();
  282. this.getTrackList();
  283. },
  284. handleCalendarClose() {
  285. this.calendarVisible = false;
  286. }
  287. }
  288. }
  289. </script>
  290. <style>
  291. .content {
  292. padding-top: 100rpx;
  293. }
  294. .fixed-bottom {
  295. position: fixed;
  296. left: 0;
  297. right: 0;
  298. top: 0;
  299. padding: 24rpx;
  300. background-color: #fff;
  301. display: flex;
  302. flex-direction: row;
  303. align-items: center;
  304. }
  305. </style>