-
Notifications
You must be signed in to change notification settings - Fork 444
/
Animate a GPS trace.html
303 lines (241 loc) · 13.3 KB
/
Animate a GPS trace.html
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<!DOCTYPE html>
<html lang="en">
<head>
<title>Animate a GPS trace - Azure Maps Web SDK Samples</title>
<meta charset="utf-8" />
<link rel="shortcut icon" href="/favicon.ico"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="This sample shows how to smoothly animate a symbol along a route path taking into consideration timestamps for each point in the route path." />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, animate, animation, symbols, pushpins, markers, pins, line, linestring, polyline" />
<meta name="author" content="Microsoft Azure Maps" /><meta name="version" content="1.0" />
<meta name="screenshot" content="screenshot.gif" />
<!-- Add references to the Azure Maps Map control JavaScript and CSS files. -->
<link href="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.css" rel="stylesheet" />
<script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.js"></script>
<!-- Add reference to the Azure Maps Spatial IO module. -->
<script src="https://atlas.microsoft.com/sdk/javascript/spatial/0/atlas-spatial.min.js"></script>
<!-- Add reference to the animation module. -->
<script src="/lib/azure-maps/azure-maps-animations.min.js"></script>
<script>
var datasets = [
{
displayName: 'GPS Trace (GeoJSON)',
url: '/data/geojson/GpsTrace.json',
timestampProperty: 'time'
},
{
displayName: 'Hurricane Barry track (csv)',
url: '/data/SpatialCSV/hurricane_barry_track.csv',
timestampProperty: 'Date'
},
{
displayName: 'Kuhkopfsteig trail (GPX)',
url: '/data/Gpx/kuhkopfsteig-fv.gpx',
//Perform a linear interpolation on the `ele` property.
valueInterpolations: [{ propertyPath: 'ele' }]
},
{
displayName: 'Bike race GPS trace (GPX)',
url: '/data/Gpx/race-gps-trace.gpx',
//Perform a linear interpolation on the `ele` property.
valueInterpolations: [{ propertyPath: 'ele' }]
}
];
var speed = 16;
var map, pin, datasource, pinSource;
var animation, infoPanel, controls;
function getMap() {
//Cache reference to DOM elements for performance.
infoPanel = document.getElementById('infoPanel');
controls = document.getElementById('controls');
//Populate the list of datasets.
var html = ['<select onchange="datasetChanged(this)" title="select dataset">'];
for(var i=0; i < datasets.length; i++){
html.push(`<option value="${i}">${datasets[i].displayName}</option>`);
}
html.push('</select>');
document.getElementById('datasetList').innerHTML = html.join('');
//Initialize a map instance.
map = new atlas.Map('myMap', {
view: 'Auto',
//Add authentication details for connecting to Azure Maps.
authOptions: {
//Use Microsoft Entra ID authentication.
authType: 'anonymous',
clientId: 'e6b6ab59-eb5d-4d25-aa57-581135b927f0', //Your Azure Maps client id for accessing your Azure Maps account.
getToken: function (resolve, reject, map) {
//URL to your authentication service that retrieves an Microsoft Entra ID Token.
var tokenServiceUrl = 'https://samples.azuremaps.com/api/GetAzureMapsToken';
fetch(tokenServiceUrl).then(r => r.text()).then(token => resolve(token));
}
//Alternatively, use an Azure Maps key. Get an Azure Maps key at https://azure.com/maps. NOTE: The primary key should be used as the key.
//authType: 'subscriptionKey',
//subscriptionKey: '[YOUR_AZURE_MAPS_KEY]'
}
});
//Wait until the map resources are ready.
map.events.add('ready', function () {
//Load a custom image icon into the map resources.
map.imageSprite.createFromTemplate('arrow-icon', 'marker-arrow', 'teal', '#fff').then(function () {
//Create a data source and add it to the map.
datasource = new atlas.source.DataSource();
//Create a second data source for the animated pin.
pinSource = new atlas.source.DataSource();
map.sources.add([datasource, pinSource]);
//Add a simple data layer for rendering the data.
layer = new atlas.layer.SimpleDataLayer(datasource);
map.layers.add(layer);
//Create a layer to render a symbol which we will animate.
map.layers.add(new atlas.layer.SymbolLayer(pinSource, null, {
iconOptions: {
//Pass in the id of the custom icon that was loaded into the map resources.
image: 'arrow-icon',
//Anchor the icon to the center of the image.
anchor: 'center',
//Rotate the icon based on the rotation property on the point data.
//The arrow icon being used in this case points down, so we have to rotate it 180 degrees.
rotation: ['+', 180, ['get', 'heading']],
//Have the rotation align with the map.
rotationAlignment: 'map',
//For smoother animation, ignore the placement of the icon. This skips the label collision calculations and allows the icon to overlap map labels.
ignorePlacement: true,
//For smoother animation, allow symbol to overlap all other symbols on the map.
allowOverlap: true
},
textOptions: {
//For smoother animation, ignore the placement of the text. This skips the label collision calculations and allows the text to overlap map labels.
ignorePlacement: true,
//For smoother animation, allow text to overlap all other symbols on the map.
allowOverlap: true
}
}));
loadGPSTrace(0);
});
});
}
function datasetChanged(elm){
loadGPSTrace(parseInt(elm.value));
}
function loadGPSTrace(idx){
controls.style.display = 'none';
datasource.clear();
pinSource.clear();
infoPanel.innerText = '';
if(animation){
map.events.remove('onprogress', animation, onAnimationProgress);
animation.dispose();
}
var dataset = datasets[idx];
if(dataset){
//Read an XML file from a URL or pass in a raw XML string.
atlas.io.read(dataset.url, {
//If the dataset is a GPX file, track and routes will capture waypoints for each point in the path.
capturePathWaypoints: true
}).then(r => {
if (r) {
//Add the feature data to the data source.
datasource.add(r);
//If bounding box information is known for data, set the map view to it.
if (r.bbox) {
map.setCamera({ bounds: r.bbox, padding: 50 });
}
//Extract route waypoints from the features.
var route = atlas.animations.extractRoutePoints(r, dataset.timestampProperty);
if(route){
//Create a pin and wrap with the shape class and add to data source.
pin = new atlas.Shape(route[0]);
pinSource.add(pin);
//Create the animation.
animation = atlas.animations.moveAlongRoute(route, pin, {
//Capture metadata so that data driven styling can be done.
captureMetadata: true,
speedMultiplier: speed,
//Provide details on any interpolations to perform on property values.
valueInterpolations: dataset.valueInterpolations,
//If following enabled, add a map to the animation.
map: (document.getElementById('followSymbol').checked)? map: null,
//Camera options to use when following.
zoom: 15,
pitch: 45
});
infoPanel.innerText = new Date(animation.getTimeSpan().begin).toUTCString();
map.events.add('onprogress', animation, onAnimationProgress);
controls.style.display = '';
}
}
});
}
}
function onAnimationProgress(e){
//Display the event data and some other info.
var html = [`${new Date(e.timestamp).toUTCString()}<br/><br/><b>Event details:</b><br/><table>`];
Object.keys(e).forEach(key => {
html.push(`<tr><td>${key}</td><td>${e[key]}</td></tr>`);
});
//Calculate speed in KM/h so its more understandable to the user.
html.push(`<tr><td>Speed (converted)</td><td>${Math.round(atlas.math.convertSpeed(e.speed, 'metersPerSecond', 'kilometersPerHour')* 100)/100} km/h</td></tr>`);
html.push('</table>');
infoPanel.innerHTML = html.join('');
}
function updateAnimation(fn){
if(animation && animation[fn]){
animation[fn]();
}
}
function toggleFollow(){
if(animation){
animation.setOptions({
map: (animation.getOptions().map)? null : map
});
}
}
function setSpeed(e){
speed = parseFloat(e.value);
if(animation){
animation.setOptions({
speedMultiplier: speed
});
}
}
</script>
</head>
<body onload="getMap()">
<div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div>
<div style="position:absolute;top:15px;left:15px;border-radius:5px;padding:5px;background-color:white;">
<b>Datasets:</b>
<br/><br/>
<div id="datasetList"></div>
<br/>
<div id="controls" style="display:none">
<b>Animation controls:</b>
<br/><br/>
<input type="button" value="Play" onclick="updateAnimation('play')" />
<input type="button" value="Pause" onclick="updateAnimation('pause')" />
<input type="button" value="Stop" onclick="updateAnimation('stop')"/>
<input type="button" value="Reset" onclick="updateAnimation('reset')"/>
<br/><br/>
<b>Follow:</b> <input id="followSymbol" type="checkbox" checked onclick="toggleFollow()" title="Follow"/>
<br/><br/>
<b>Speed:</b>
<select onchange="setSpeed(this)" title="Speed">
<option value="0.5">0.5x</option>
<option value="1">1x</option>
<option value="2">2x</option>
<option value="5">5x</option>
<option value="15" selected="selected">15x</option>
<option value="30">30x</option>
<option value="60">60x</option>
<option value="100">100x</option>
<option value="3600">3600x</option>
</select>
</div>
</div>
<div id="infoPanel" style="position:absolute;top:15px;right:15px;border-radius:5px;padding:5px;background-color:white;max-width:300px;overflow:hidden;"></div>
<fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;">
<legend>Animate a GPS trace</legend>
This sample shows how to smoothly animate a symbol along a route path taking into consideration timestamps for each point in the route path.
This sample uses the <a href="https://docs.microsoft.com/en-us/azure/azure-maps/how-to-use-spatial-io-module" target="_blank" title="Azure Maps Spatial IO module">Azure Maps Spatial IO module</a>
and the open source <a href="https://github.com/Azure-Samples/azure-maps-animations" target="_blank" title="Azure Maps Animation module">Azure Maps Animation module</a>
</fieldset>
</body>
</html>