-
Notifications
You must be signed in to change notification settings - Fork 456
/
Copy pathCurved lines.html
199 lines (178 loc) · 8.81 KB
/
Curved lines.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Curved lines - 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 three different ways to create curved lines in Azure Maps." />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, linestring, arrows, path, symbols, linelayer, curve, curves, geodesic" />
<meta name="author" content="Microsoft Azure Maps" />
<meta name="version" content="1.0" />
<meta name="screenshot" content="screenshot.jpg" />
<!-- 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 references to two additional line calculation classes. -->
<script src="/spatial-math/curved-lines/js/CubicBezier.js"></script>
<script src="/spatial-math/curved-lines/js/RoundedCorners.js"></script>
<script>
var map, datasource;
var originalLine = {
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: [
[-122.203812, 47.61456],
[-122.203831, 47.615602],
[-122.203945, 47.615593],
[-122.20397, 47.616131],
[-122.204831, 47.616122],
[-122.204882, 47.616763],
[-122.205356, 47.616754],
[-122.205394, 47.617304],
[-122.203482, 47.617317],
[-122.203457, 47.616942],
[-122.203318, 47.616805],
[-122.203115, 47.616178],
[-122.20183, 47.616203]
]
},
properties: {
pathType: 'original'
}
};
function getMap() {
//Initialize a map instance.
map = new atlas.Map('myMap', {
center: [-122.204396, 47.615568],
zoom: 16,
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 () {
//Create a data source and add it to the map.
datasource = new atlas.source.DataSource();
map.sources.add(datasource);
//Create a layer for visualizing the lines on the map.
map.layers.add(new atlas.layer.LineLayer(datasource, null, {
strokeWidth: [
'match',
['get', 'pathType'],
'original', 10,
4
],
strokeColor: [
'match',
['get', 'pathType'],
'original', 'Black',
'cardinalSpline', 'DodgerBlue',
'cubicBezier', 'DeepPink',
'roundedCorners', 'LightSeaGreen',
'black'
]
}));
recalculateCurves();
});
}
function recalculateCurves() {
var tension = parseFloat(document.getElementById('Tension').value);
var nodeSize = parseInt(document.getElementById('Nodesize').value);
var lines = [originalLine];
//Create a line using the rounded corners method.
lines.push(new atlas.data.Feature(
new atlas.data.LineString(RoundedCorners(originalLine.geometry.coordinates, 5, nodeSize)), {
pathType: 'roundedCorners'
}));
//Create a line using the cardinal spline method.
lines.push(new atlas.data.Feature(
new atlas.data.LineString(atlas.math.getCardinalSpline(originalLine.geometry.coordinates, tension, nodeSize)), {
pathType: 'cardinalSpline'
}));
//Cretae a line using the cubic bezier method.
lines.push(new atlas.data.Feature(
CubicBezier.getLine(originalLine.geometry.coordinates, tension, nodeSize), {
pathType: 'cubicBezier'
}));
//Add lines to the data source.
datasource.setShapes(lines);
}
</script>
<style>
.panel {
position: absolute;
top: 10px;
left: 10px;
padding: 10px;
background-color: white;
border-radius: 10px;
}
</style>
</head>
<body onload="getMap()">
<div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div>
<div class="panel">
<table>
<tr>
<td>Tension:</td>
<td>
<form oninput="tension.value=Tension.value">
<input type="range" id="Tension" value="0.5" min="-1" max="1" step="0.05" oninput="recalculateCurves()" onchange="recalculateCurves()" />
<output name="tension" for="Tension">0.5</output>
</form>
</td>
</tr>
<tr>
<td>Node size:</td>
<td>
<form oninput="nodeSize.value=Nodesize.value">
<input type="range" id="Nodesize" value="15" min="2" max="100" step="1" oninput="recalculateCurves()" onchange="recalculateCurves()" />
<output name="nodeSize" for="Nodesize">15</output>
</form>
</td>
</tr>
<tr>
<td><svg viewBox="0 0 20 5" xmlns="http://www.w3.org/2000/svg"><line x1="5" y1="2" x2="15" y2="2" stroke="Black" /></svg></td>
<td>Original line</td>
</tr>
<tr>
<td><svg viewBox="0 0 20 5" xmlns="http://www.w3.org/2000/svg"><line x1="5" y1="2" x2="15" y2="2" stroke="DodgerBlue" /></svg></td>
<td>Cardinal Spline</td>
</tr>
<tr>
<td><svg viewBox="0 0 20 5" xmlns="http://www.w3.org/2000/svg"><line x1="5" y1="2" x2="15" y2="2" stroke="DeepPink" /></svg></td>
<td>Cubic Bezier</td>
</tr>
<tr>
<td><svg viewBox="0 0 20 5" xmlns="http://www.w3.org/2000/svg"><line x1="5" y1="2" x2="15" y2="2" stroke="LightSeaGreen" /></svg></td>
<td>Rounded corners</td>
</tr>
</table>
</div>
<fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;">
<legend>Curved lines</legend>
This sample shows three different ways to create curved lines in Azure Maps. Note that there is no curved line class built into Azure Maps and instead you need to calculate points the approximate the path of a curved line.
The following three types of line curves are demonstrated in this sample:
<ul>
<li>Cardinal Spline - A curve calculation built into the Azure Maps math library.</li>
<li>Cubic Bezier - Another type of line curve calculation.</li>
<li>Rounded corners - Where two lines join, a curved is calculated a set offset from the ends of the lines.</li>
</ul>
Another type of curved line that is not demonstrated in this sample is a great circle line, also known as a geodesic line. This is a line that follows the curvature of the earth and thus only noticable when lines cover a long distance. <a href="/Spatial Math/Introduction to spatial math/Introduction to spatial math.html">Here is an example of a geodesic curve.</a>
</fieldset>
</body>
</html>