-
Notifications
You must be signed in to change notification settings - Fork 445
/
Animate along a path.html
183 lines (150 loc) · 8.77 KB
/
Animate along a path.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Animate along a 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 animate a symbol along a path on the map smoothly. 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 animationTime = 15000;
var animation;
//Create an array of points to define a path to animate along.
var path = [
[-122.34758, 47.62155],
[-122.34764, 47.61859],
[-122.33787, 47.61295],
[-122.34217, 47.60964]
];
function getMap() {
//Initialize a map instance.
map = new atlas.Map('myMap', {
center: [-122.345, 47.615],
zoom: 14,
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 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
}));
//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(new atlas.data.Feature(new atlas.data.Point(path[0])));
pinSource.add(pin);
//Create the animation.
animation = atlas.animations.moveAlongPath(path, pin, {
//Capture metadata so that data driven styling can be done.
captureMetadata: true,
duration: 4000,
loop: document.getElementById('loopAnimation').checked,
reverse: document.getElementById('reverseAnimation').checked,
rotationOffset: (document.getElementById('reverseAnimation').checked)? 90: 0,
//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 path</legend>
This sample shows how to animate a symbol along a path on the map smoothly. 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>