Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rotate icon in animate a point along a route example #6553

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions docs/pages/example/animate-point-along-route.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": origin
Expand All @@ -75,9 +76,14 @@

var arc = [];

// Number of steps to use in the arc and animation, more steps means
// a smoother arc and animation, but too many steps will result in a
// low frame rate
var steps = 500;

// Draw an arc between the `origin` & `destination` of the two points
for (var i = 0; i < lineDistance; i++) {
var segment = turf.along(route.features[0], i / 1000 * lineDistance, 'kilometers');
for (var i = 0; i < lineDistance; i += lineDistance / steps) {
var segment = turf.along(route.features[0], i, 'kilometers');
arc.push(segment.geometry.coordinates);
}

Expand Down Expand Up @@ -115,7 +121,8 @@
"type": "symbol",
"layout": {
"icon-image": "airport-15",
"icon-rotate": 90,
"icon-rotate": { "type": "identity", "property": "bearing"},
"icon-rotation-alignment": "map",
"icon-allow-overlap": true
}
});
Expand All @@ -125,12 +132,19 @@
// the index to access the arc.
point.features[0].geometry.coordinates = route.features[0].geometry.coordinates[counter];

// Calculate the bearing to ensure the icon is rotated to match the route arc
// The bearing is calculate between the current point and the next point, except
// at the end of the arc use the previous point and the current point
point.features[0].properties.bearing = turf.bearing(
turf.point(route.features[0].geometry.coordinates[counter >= steps ? counter - 1 : counter]),
turf.point(route.features[0].geometry.coordinates[counter >= steps ? counter : counter + 1])
);

// Update the source with this new data.
map.getSource('point').setData(point);

// Request the next frame of animation so long as destination has not
// been reached.
if (point.features[0].geometry.coordinates[0] !== destination[0]) {
// Request the next frame of animation so long the end has not been reached.
if (counter < steps) {
requestAnimationFrame(animate);
}

Expand Down