trajectory.vue
4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<template>
<view class="container">
<view id="map" style="width:100wh;height:100vh" :lineList="lineList" :change:lineList="AMapInstance.setLineList"></view>
<view class="control fs-p20 fs-bg__white fs-radius__sm fs-size__h5">
<view class="fs-flex fs-mb20">
<view class="fs-mr20 fs-color__primary fs-border fs-px20 fs-py10 fs-radius__sm" @click="AMapInstance.startAnimation">开始动画</view>
<view class="fs-color__primary fs-border fs-px20 fs-py10 fs-radius__sm" @click="AMapInstance.pauseAnimation">暂停动画</view>
</view>
<view class="fs-flex">
<view class="fs-mr20 fs-color__primary fs-border fs-px20 fs-py10 fs-radius__sm" @click="AMapInstance.resumeAnimation">继续动画</view>
<view class="fs-color__primary fs-border fs-px20 fs-py10 fs-radius__sm" @click="AMapInstance.stopAnimation">停止动画</view>
</view>
</view>
<view class="date fs-bg__white fs-radius__sm">
<tui-input label="选择日期" placeholder="请选择" :size="28" padding="20rpx 20rpx" disabled v-model="date" @click="openDate"></tui-input>
</view>
<tui-datetime :type="2" ref="dateTime" radius @confirm="dateChange"></tui-datetime>
</view>
</template>
<script>
import { apiUserGpsList } from '@/api/work'
export default {
data() {
return {
userId: 0,
date: '',
lineList: []
}
},
onLoad(options) {
this.userId = options.id
// 获取当前日期
const currentDate = new Date()
const year = String(currentDate.getYear() + 1900) // 需要加上1900
const month = String(currentDate.getMonth() + 1).padStart(2, '0') // 需要补零
const day = String(currentDate.getDate()).padStart(2, '0') // 需要补零
this.date = year + '-' + month + '-' + day
this.getUserGpsList()
},
methods: {
// 查询轨迹列表
getUserGpsList() {
apiUserGpsList({data:{user_id:this.userId, commit_time:this.date}}).then(res => {
var lineList = []
res.data.forEach(item => {
lineList.push([item.lon, item.lat])
})
if (lineList.length == 0) {
uni.$tui.toast('该日期无轨迹信息')
}
this.lineList = lineList
})
},
// 打开日期选择
openDate() {
this.$refs.dateTime.show()
},
// 日期选择
dateChange(e) {
this.date = e.result
this.getUserGpsList()
}
}
}
</script>
<script module="AMapInstance" lang="renderjs">
export default {
data() {
return {
map: null,
marker: null,
lineList: []
}
},
mounted() {
if (window.AMap) {
// 观测更新的数据在 view 层可以直接访问到
this.initAmap()
} else {
// 动态引入较大类库避免影响页面展示
const script = document.createElement('script')
script.charset = 'utf-8'
// view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
script.src = 'https://webapi.amap.com/maps?v=2.0&key=89c6c0c88a377f3c272f85efbd54f26f&plugin=AMap.MoveAnimation' //您申请的key值
//script标签的onload事件都是在外部js文件被加载完成并执行完成后才被触发的
script.onload = () => {
window._AMapSecurityConfig = {
securityJsCode: '17410d15a21623c837ff1064a6ff18f5' //您申请的安全密钥
}
this.initAmap()
}
document.head.appendChild(script)
}
},
methods: {
// 设置数据
setLineList(newValue, oldValue) {
this.lineList = newValue || []
this.drawTrack()
},
// 初始化地图
initAmap() {
this.map = new AMap.Map("map", {
resizeEnable: true,
center: [116.397428, 39.90923],
zoom: 17
})
this.drawTrack()
},
// 绘制轨迹
drawTrack() {
this.map.clearMap()
var that = this
this.marker = new AMap.Marker({
map: that.map,
position: [that.lineList[0][0], that.lineList[0][1]],
icon: "./static/images/car.png",
offset: new AMap.Pixel(-13, -26)
})
// 绘制轨迹
const polyline = new AMap.Polyline({
map: that.map,
path: that.lineList,
showDir:true,
strokeColor: "#28F", //线颜色
strokeWeight: 6 //线宽
})
const passedPolyline = new AMap.Polyline({
map: that.map,
strokeColor: "#AF5", //线颜色
strokeWeight: 6 //线宽
})
this.marker.on('moving', function (e) {
passedPolyline.setPath(e.passedPath)
that.map.setCenter(e.target.getPosition(), true)
})
this.map.setFitView()
},
// 开始动画
startAnimation() {
this.marker.moveAlong(this.lineList, {
// 每一段的时长
duration: 500, //可根据实际采集时间间隔设置
// JSAPI2.0 是否延道路自动设置角度在 moveAlong 里设置
autoRotation: true
})
},
// 暂停动画
pauseAnimation () {
this.marker.pauseMove()
},
// 继续动画
resumeAnimation () {
this.marker.resumeMove()
},
// 停止动画
stopAnimation () {
this.marker.stopMove()
}
}
}
</script>
<style lang="scss" scoped>
.date {
width: 360rpx;
position: fixed;
top: 10rpx;
left: 10rpx;
}
.control {
position: fixed;
bottom: 34rpx;
right: 30rpx;
}
</style>