-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflytoanimation.html
91 lines (83 loc) · 3.01 KB
/
flytoanimation.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Animate Map Camera Sequentially</title>
<meta charset='utf-8'>
<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: [-87.62712, 41.89033], // Initial center (Chicago)
zoom: 15.5,
pitch: 45
});
// Coordinates of multiple markers (lat, long)
const markers = [
[-87.62712, 41.89033], // Initial marker (Chicago)
[-87.631, 41.892], // Example marker 1
[-87.620, 41.895], // Example marker 2
[-87.630, 41.888] // Example marker 3
];
// Add markers to the map
markers.forEach((coord) => {
new maplibregl.Marker().setLngLat(coord).addTo(map);
});
// Function to rotate the camera around the initial marker
function rotateCamera(timestamp) {
map.rotateTo((timestamp / 100) % 360, { duration: 0 });
requestAnimationFrame(rotateCamera);
}
// Function to animate the camera to each marker one by one
function flyToMarkers(markerIndex) {
if (markerIndex < markers.length) {
// Fly to the current marker
map.flyTo({
center: markers[markerIndex],
zoom: 20,
pitch: 45,
bearing: 0,
duration: 2000 // 2 seconds to fly to each marker
});
// Set timeout to fly to the next marker after 3 seconds (includes fly time)
setTimeout(() => {
flyToMarkers(markerIndex + 1);
}, 3000); // Wait 3 seconds before going to the next marker
} else {
// After visiting all markers, return to the initial marker
returnToInitialMarker();
}
}
// Return to the initial marker and start rotating around it
function returnToInitialMarker() {
const initialMarker = markers[0]; // First marker in the array
map.flyTo({
center: initialMarker,
zoom: 16,
pitch: 45,
bearing: 0, // Reset the bearing
duration: 2000 // Animate over 2 seconds
});
// Start rotating the camera after reaching the initial marker
setTimeout(() => {
rotateCamera(0);
}, 2000); // Delay rotation to allow flyTo animation to complete
}
// Wait for the map to load
map.on('load', () => {
// Start animating the camera to each marker
flyToMarkers(0); // Start from the first marker
});
</script>
</body>
</html>