-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathanimation.html
108 lines (96 loc) · 3.32 KB
/
pathanimation.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Trace Path with Camera Animation</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/maplibre-gl.css" />
<script src="https://unpkg.com/[email protected]/dist/maplibre-gl.js"></script>
<style>
body { margin: 0; padding: 0; }
html, body, #map { height: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script>
// Initialize the map
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets/style.json?key=get_your_own_OpIi9ZULNHzrESv6T2vL',
center: [-118.809361, 34.003098], // Initial map center
zoom: 12,
pitch: 45
});
// Predefined route coordinates (can be adjusted to real paths)
const routeCoordinates = [
[-118.809361, 34.003098], // Point Dume Beach
[-118.47549, 33.987367], // Venice Skate Park
[-116.830104, 33.022843], // San Diego Skydive
[-119.600492, 37.742371] // Yosemite National Park
];
// Prepare an empty GeoJSON object for the route
const routeGeoJSON = {
'type': 'FeatureCollection',
'features': [{
'type': 'Feature',
'geometry': {
'type': 'LineString',
'coordinates': []
}
}]
};
let currentStep = 0;
let totalSteps = routeCoordinates.length;
// Add route source and layer when the map loads
map.on('load', () => {
// Add the route source
map.addSource('route', {
'type': 'geojson',
'data': routeGeoJSON
});
// Add a line layer to visualize the route
map.addLayer({
'id': 'route',
'type': 'line',
'source': 'route',
'layout': {
'line-join': 'round',
'line-cap': 'round'
},
'paint': {
'line-color': '#ff0000',
'line-width': 4
}
});
// Start tracing and animating the path
traceRoute();
});
// Function to trace the route step by step
function traceRoute() {
if (currentStep < totalSteps) {
const nextCoord = routeCoordinates[currentStep];
routeGeoJSON.features[0].geometry.coordinates.push(nextCoord);
// Update the route data to show the new segment of the path
map.getSource('route').setData(routeGeoJSON);
// Move the camera along the new point
map.flyTo({
center: nextCoord,
zoom: 14,
speed: 0.5, // Controls the speed of the camera
curve: 1, // Smoothness of camera animation
pitch: 60,
bearing: 0,
essential: true
});
currentStep++;
// Continue tracing the route with a small delay for each step
setTimeout(traceRoute, 2000); // Adjust the delay for smoother or faster animation
} else {
// Once the route is fully traced, you can stop or perform other actions (like rotating camera)
console.log('Route tracing complete');
}
}
</script>
</body>
</html>