-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
244 lines (230 loc) · 7.83 KB
/
index.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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Para Po! Route Generator</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- Import Mapbox GL JS -->
<script src="https://api.tiles.mapbox.com/mapbox-gl-js/v3.3.0/mapbox-gl.js"></script>
<link
href="https://api.tiles.mapbox.com/mapbox-gl-js/v3.3.0/mapbox-gl.css"
rel="stylesheet"
/>
<!-- Import Mapbox GL Draw -->
<script src="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.0.9/mapbox-gl-draw.js"></script>
<link
rel="stylesheet"
href="https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-draw/v1.0.9/mapbox-gl-draw.css"
type="text/css"
/>
<style>
body {
margin: 0;
padding: 0;
font-family: sans-serif;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
.info-box {
position: absolute;
margin: 20px;
width: 25%;
top: 0;
bottom: 20px;
padding: 20px;
background-color: #fff;
overflow-y: scroll;
}
</style>
</head>
<body>
<!-- Create a container for the map -->
<div id="map"></div>
<!-- Create a container for the instructions and directions -->
<div class="info-box">
<p>
Draw your route using the draw tools on the right. To get the most
accurate route match, draw points at regular intervals.
</p>
<div id="directions"></div>
</div>
<script>
// Add your Mapbox access token
mapboxgl.accessToken = 'pk.eyJ1IjoiY29sc3RvbiIsImEiOiJjbHQ1dDZqdjYwNGtnMmtvMG9ucm14ZjNjIn0.4Ic21BnlAyeTHEtAatCbnQ';
const map = new mapboxgl.Map({
container: 'map', // Specify the container ID
style: 'mapbox://styles/mapbox/streets-v12', // Specify which map style to use
center: [120.59370720064805, 16.410433402784093], // Specify the starting position
zoom: 14.5 // Specify the starting zoom
});
const draw = new MapboxDraw({
// Instead of showing all the draw tools, show only the line string and delete tools
displayControlsDefault: false,
controls: {
line_string: true,
trash: true
},
// Set the draw mode to draw LineStrings by default
defaultMode: 'draw_line_string',
styles: [
// Set the line style for the user-input coordinates
{
'id': 'gl-draw-line',
'type': 'line',
'filter': [
'all',
['==', '$type', 'LineString'],
['!=', 'mode', 'static']
],
'layout': {
'line-cap': 'round',
'line-join': 'round'
},
'paint': {
'line-color': '#438EE4',
'line-dasharray': [0.2, 2],
'line-width': 2,
'line-opacity': 0.7
}
},
// Style the vertex point halos
{
'id': 'gl-draw-polygon-and-line-vertex-halo-active',
'type': 'circle',
'filter': [
'all',
['==', 'meta', 'vertex'],
['==', '$type', 'Point'],
['!=', 'mode', 'static']
],
'paint': {
'circle-radius': 12,
'circle-color': '#FFF'
}
},
// Style the vertex points
{
'id': 'gl-draw-polygon-and-line-vertex-active',
'type': 'circle',
'filter': [
'all',
['==', 'meta', 'vertex'],
['==', '$type', 'Point'],
['!=', 'mode', 'static']
],
'paint': {
'circle-radius': 8,
'circle-color': '#438EE4'
}
}
]
});
// Add the draw tool to the map
map.addControl(draw);
// Add create, update, or delete actions
map.on('draw.create', updateRoute);
map.on('draw.update', updateRoute);
map.on('draw.delete', removeRoute);
// Use the coordinates you just drew to make the Map Matching API request
function updateRoute() {
removeRoute(); // Overwrite any existing layers
const profile = 'driving'; // Set the profile
// Get the coordinates
const data = draw.getAll();
const lastFeature = data.features.length - 1;
const coords = data.features[lastFeature].geometry.coordinates;
// Format the coordinates
const newCoords = coords.join(';');
// Set the radius for each coordinate pair to 25 meters
const radius = coords.map(() => 25);
getMatch(newCoords, radius, profile);
}
// Make a Map Matching request
async function getMatch(coordinates, radius, profile) {
// Separate the radiuses with semicolons
const radiuses = radius.join(';');
// Create the query
const query = await fetch(
`https://api.mapbox.com/matching/v5/mapbox/${profile}/${coordinates}?geometries=geojson&radiuses=${radiuses}&steps=true&access_token=${mapboxgl.accessToken}`,
{ method: 'GET' }
);
const response = await query.json();
// Handle errors
if (response.code !== 'Ok') {
alert(
`${response.code} - ${response.message}.\n\nFor more information: https://docs.mapbox.com/api/navigation/map-matching/#map-matching-api-errors`
);
return;
}
const coords = response.matchings[0].geometry;
// Draw the route on the map
addRoute(coords);
getInstructions(response.matchings[0], coordinates);
}
function getInstructions(data, coordinates) {
// Target the sidebar to add the instructions
const directions = document.getElementById('directions');
// Format the coordinates
const coords = coordinates.split(';').map(pair => {
const [lng, lat] = pair.split(',').map(Number);
return ` [${lng}, ${lat}]`;
}).join(',<br>');
const formattedCoords = `[<br>${coords}<br>]`;
let tripDirections = '';
// Output the instructions for each step of each leg in the response object
for (const leg of data.legs) {
const steps = leg.steps;
for (const step of steps) {
tripDirections += `<li>${step.maneuver.instruction}</li>`;
}
}
directions.innerHTML = `<p><strong>Trip duration: ${Math.floor(
data.duration / 60
)} min.</strong></p>
<ol>${tripDirections}</ol>
<p><strong>Coordinates:</strong></p>
<div style="font-family: monospace; white-space: pre;">${formattedCoords}</div>`;
}
// Draw the Map Matching route as a new layer on the map
function addRoute(coords) {
// If a route is already loaded, remove it
if (map.getSource('route')) {
map.removeLayer('route');
map.removeSource('route');
} else {
map.addLayer({
'id': 'route',
'type': 'line',
'source': {
'type': 'geojson',
'data': {
'type': 'Feature',
'properties': {},
'geometry': coords
}
},
'layout': {
'line-join': 'round',
'line-cap': 'round'
},
'paint': {
'line-color': '#03AA46',
'line-width': 8,
'line-opacity': 0.8
}
});
}
}
// If the user clicks the delete draw button, remove the layer if it exists
function removeRoute() {
if (!map.getSource('route')) return;
map.removeLayer('route');
map.removeSource('route');
}
</script>
</body>
</html>