-
Notifications
You must be signed in to change notification settings - Fork 472
Expand file tree
/
Copy pathAnimated traffic flow.html
More file actions
190 lines (161 loc) · 9.64 KB
/
Animated traffic flow.html
File metadata and controls
190 lines (161 loc) · 9.64 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Animated traffic flow - 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 the flow of traffic relative to the congestion level using the flowing dashed line animation." />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, animation, animate, animations, traffic flow, traffic, flow, dashed" />
<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, layer, animation;
function getMap() {
//Initialize a map instance.
map = new atlas.Map('myMap', {
center: [-74, 40.72],
zoom: 15,
style: 'grayscale_dark',
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 () {
//Create a vector tile source and add it to the map.
datasource = new atlas.source.VectorTileSource(null, {
tiles: ['https://{azMapsDomain}/traffic/flow/tile/pbf?api-version=1.0&style=relative&zoom={z}&x={x}&y={y}'],
maxZoom: 22
});
map.sources.add(datasource);
//Common style options for traffic background colors.
var trafficBackgroundOptions = {
//The name of the data layer within the data source to pass into this rendering layer.
sourceLayer: 'Traffic flow',
//Color the roads based on the level of traffic.
strokeColor: [
'step',
['get', 'traffic_level'],
'#6B0512', //Dark red
0.01, '#EE2F53', //Red
0.8, 'orange', //Orange
1, "#66CC99" //Green
],
//Scale the width of roads based on the level of traffic.
strokeWidth: [
'interpolate',
['exponential', 2],
['zoom'],
12, 3,
17, 9
],
};
//Create two line layer for the base traffic flow color. One layer for both direction traffic data, and one layer for single line traffic data.
map.layers.add([
new atlas.layer.LineLayer(datasource, null, Object.assign({
//For traffic data that represents one side of the road, offset it.
offset: [
'interpolate',
['exponential', 2],
['zoom'],
12, 2,
17, 6
],
filter: ['==', ['get', 'traffic_road_coverage'], 'one_side']
}, trafficBackgroundOptions)),
new atlas.layer.LineLayer(datasource, null, Object.assign({
filter: ['==', ['get', 'traffic_road_coverage'], 'full']
}, trafficBackgroundOptions))
], 'labels');
//Common style options for traffic flow dashed lines.
var trafficFLowLineOptions = {
sourceLayer: 'Traffic flow',
strokeColor: 'black',
//Scale the width of roads based on the level of traffic.
strokeWidth: [
'interpolate',
['exponential', 2],
['zoom'],
12, 1,
17, 4
]
};
//Create an offset for the layers that has two directional traffic data.
var offsetExp = [
'interpolate',
['exponential', 2],
['zoom'],
12, 3,
17, 7
];
//Create line layers for the different levels of traffic flow.
var oneSideSlowFlowLayer = new atlas.layer.LineLayer(datasource, null, Object.assign({
offset: offsetExp,
filter: ['all', ['==', ['get', 'traffic_road_coverage'], 'one_side'], ['>', ['get', 'traffic_level'], 0], ['<=', ['get', 'traffic_level'], 0.01]]
}, trafficFLowLineOptions));
var slowFlowLayer = new atlas.layer.LineLayer(datasource, null, Object.assign({
filter: ['all', ['==', ['get', 'traffic_road_coverage'], 'full'], ['>', ['get', 'traffic_level'], 0], ['<=', ['get', 'traffic_level'], 0.01]]
}, trafficFLowLineOptions));
var oneSideMidFlowLayer = new atlas.layer.LineLayer(datasource, null, Object.assign({
offset: offsetExp,
filter: ['all', ['==', ['get', 'traffic_road_coverage'], 'one_side'], ['>', ['get', 'traffic_level'], 0.01], ['<=', ['get', 'traffic_level'], 0.8]]
}, trafficFLowLineOptions));
var midFlowLayer = new atlas.layer.LineLayer(datasource, null, Object.assign({
filter: ['all', ['==', ['get', 'traffic_road_coverage'], 'full'], ['>', ['get', 'traffic_level'], 0.01], ['<=', ['get', 'traffic_level'], 0.8]]
}, trafficFLowLineOptions));
var oneSideFastFlowLayer = new atlas.layer.LineLayer(datasource, null, Object.assign({
offset: offsetExp,
filter: ['all', ['==', ['get', 'traffic_road_coverage'], 'one_side'], ['>', ['get', 'traffic_level'], 0.8]]
}, trafficFLowLineOptions));
var fastFlowLayer = new atlas.layer.LineLayer(datasource, null, Object.assign({
filter: ['all', ['==', ['get', 'traffic_road_coverage'], 'full'], ['>', ['get', 'traffic_level'], 0.8]]
}, trafficFLowLineOptions));
//Add the layers below the labels to make the map clearer.
map.layers.add([oneSideSlowFlowLayer, slowFlowLayer, oneSideMidFlowLayer, midFlowLayer, oneSideFastFlowLayer, fastFlowLayer], 'labels');
//Create a moving dashed line animation for each of the flow layers, but with different speedMultipliers.
//Reverse the animation direction as it appears to ensure the correct flow directions for different side of the road for most countries (drive on the right side).
var animationOptions = {
gapLength: 2,
dashLength: 2,
duration: 2000,
autoPlay: true,
loop: true,
reverse: true
};
atlas.animations.flowingDashedLine(oneSideSlowFlowLayer, Object.assign({ speedMultiplier: 0.25 }, animationOptions));
atlas.animations.flowingDashedLine(slowFlowLayer, Object.assign({ speedMultiplier: 0.25 }, animationOptions));
atlas.animations.flowingDashedLine(oneSideMidFlowLayer, Object.assign({ speedMultiplier: 1 }, animationOptions));
atlas.animations.flowingDashedLine(midFlowLayer, Object.assign({ speedMultiplier: 1 }, animationOptions));
atlas.animations.flowingDashedLine(oneSideFastFlowLayer, Object.assign({ speedMultiplier: 4 }, animationOptions));
atlas.animations.flowingDashedLine(fastFlowLayer, Object.assign({ speedMultiplier: 4 }, animationOptions));
});
}
</script>
</head>
<body onload="getMap()">
<div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div>
<fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;">
<legend>Animated traffic flow</legend>
This sample shows how to animate the flow of traffic relative to the congestion level using the flowing dashed line animation.
This sample uses the open source <a href="https://github.com/Azure-Samples/azure-maps-animations" target="_blank">Azure Maps Animation module</a>
</fieldset>
</body>
</html>