Created
May 14, 2019 16:28
-
-
Save BetterProgramming/1bb4fc8d65af470332e3498827865ad9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { lineString, along, lineDistance } from '@turf/turf'; | |
const OPTIONS = { units: 'feet' }; | |
// 30.1 seconds, the .1 is to allow a buffer for the next set of cords to load | |
// I know it's not exact, but it's close :) | |
const STEPS = 30100; | |
//inside component function | |
useEffect(() => { | |
let arc = []; | |
let startTime = 0; | |
if (!locations.length) {// exit if no cords in array | |
return; | |
} | |
const line = lineString(locations); // our array of lat/lngs | |
const distance = lineDistance(line, OPTIONS); | |
for (let i = 0; i < distance; i += distance / STEPS) { | |
let segment = along(line, i, OPTIONS); | |
arc.push(segment.geometry.coordinates); | |
} | |
function animate(timestamp) {// animate function to set location | |
const runtime = timestamp - startTime; | |
const timeStep = Math.round(runtime); | |
setCurrentLocation(arc[timeStep] || arc[arc.length - 1]); | |
if (timeStep <= STEPS) { | |
window.requestAnimationFrame(animate); | |
} | |
} | |
window.cancelAnimationFrame(frameId); | |
frameId = window.requestAnimationFrame(timeStamp => { | |
startTime = timeStamp; | |
animate(timeStamp); | |
}); | |
}, | |
[locations] | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment