-
Notifications
You must be signed in to change notification settings - Fork 471
Expand file tree
/
Copy pathAnimate along a route path.html
More file actions
199 lines (161 loc) · 10.2 KB
/
Animate along a route path.html
File metadata and controls
199 lines (161 loc) · 10.2 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<!DOCTYPE html>
<html lang="en">
<head>
<title>Animate along a route path - 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. This sample also includes controls and options for the animation." />
<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 animation module. -->
<script src="/lib/azure-maps/azure-maps-animations.min.js"></script>
<script>
var map, pin, lineSource, pinSource;
var animation;
//Create an array of point features with timestamp information to define a route to animate along.
//To animate a route, there must be a _timestamp property that has a value from Date.getTime().
var routePoints = [
new atlas.data.Feature(new atlas.data.Point([-122.34758, 47.62155]), { timestamp: new Date('Tue, 18 Aug 2020 00:53:53 GMT').getTime() }),
new atlas.data.Feature(new atlas.data.Point([-122.34764, 47.61859]), { timestamp: new Date('Tue, 18 Aug 2020 00:54:53 GMT').getTime() }),
new atlas.data.Feature(new atlas.data.Point([-122.33787, 47.61295]), { timestamp: new Date('Tue, 18 Aug 2020 00:56:53 GMT').getTime() }),
new atlas.data.Feature(new atlas.data.Point([-122.34217, 47.60964]), { timestamp: new Date('Tue, 18 Aug 2020 00:59:53 GMT').getTime() })
];
function getMap() {
//Initialize a map instance.
map = new atlas.Map('myMap', {
center: [-122.345, 47.615],
zoom: 14,
view: 'Auto',
language: 'Auto',
//Add authentication details for connecting to Azure Maps.
authOptions: {
// Use SAS token for authentication
authType: 'sas',
getToken: function (resolve, reject, map) {
// URL to your authentication service that retrieves a SAS Token
var tokenServiceUrl = 'https://samples.azuremaps.com/api/GetAzureMapsSasToken';
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 data sources and add them to the map.
lineSource = new atlas.source.DataSource();
pinSource = new atlas.source.DataSource();
map.sources.add([lineSource, pinSource]);
//Create a layer to render the path.
map.layers.add(new atlas.layer.LineLayer(lineSource, null, {
strokeColor: 'DodgerBlue',
strokeWidth: 3
}));
//Extract the positions to highlight the full route on the map as a line.
var path = [];
routePoints.forEach(f => {
path.push(f.geometry.coordinates);
});
//Create a line for the path and add it to the data source.
lineSource.add(new atlas.data.LineString(path));
//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
}
}));
//Create a pin and wrap with the shape class and add to data source.
pin = new atlas.Shape(routePoints[0]);
pinSource.add(pin);
//Extract timestamp information from a custom property of the point features.
//By default the `moveAlongRoute` function looks for a `_timestamp` property on the point features that is a Date.getTime() value.
//If your points already have a `_timestamp` property with the correct value type, you can skip this extraction step.
routePoints = atlas.animations.extractRoutePoints(routePoints, 'timestamp');
//Create the animation.
animation = atlas.animations.moveAlongRoute(routePoints, pin, {
//Capture metadata so that data driven styling can be done.
captureMetadata: true,
loop: document.getElementById('loopAnimation').checked,
reverse: document.getElementById('reverseAnimation').checked,
rotationOffset: (document.getElementById('reverseAnimation').checked)? 90: 0,
//Animate such that 1 second of animation time = 1 minute of data time.
speedMultiplier: 60,
//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,
rotate: true
});
});
});
}
function toggleFollow(){
animation.setOptions({
map: (animation.getOptions().map)? null : map
});
}
function toggleFollowOffset(){
animation.setOptions({
rotationOffset: (animation.getOptions().rotationOffset === 0)? 90 : 0
});
}
function toggleLooping(){
animation.setOptions({
loop: !animation.getOptions().loop
});
}
function toggleReverse(){
animation.setOptions({
reverse: !animation.getOptions().reverse
});
}
</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;">
<input type="button" value="Play" onclick="animation.play()" title="Play" />
<input type="button" value="Pause" onclick="animation.pause()" title="Pause" />
<input type="button" value="Stop" onclick="animation.stop()" title="Stop" />
<input type="button" value="Reset" onclick="animation.reset()" title="Reset" />
<br/><br/>
Follow: <input id="followSymbol" type="checkbox" onclick="toggleFollow()" title="Follow"/><br/>
Follow offset: <input id="followOffset" type="checkbox" onclick="toggleFollowOffset()" title="Follow offset"/><br/>
Loop: <input id="loopAnimation" type="checkbox" onclick="toggleLooping()" title="Loop"/><br/>
Reverse: <input id="reverseAnimation" type="checkbox" onclick="toggleReverse()" title="Reverse"/>
</div>
<fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;">
<legend>Animate along a route path</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 also includes controls and options for the animation.
This sample uses 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>