Randomly generate running and cycling routes using Mapbox and Turf.js
I'm not paying you to go on a run!
One day, I got bored with my normal running routes so I googled "Random running route generator". I stumbled upon a website called https://routeshuffle.com/.
While the functionality was pretty neat, I soon realized that I wouldn't be able to export the route into Google Maps. Whatever.
Then I realized that I wasn't able so save a route either.
I wasn't even able to log in and make a free account!
Thus, Freeshuffle was born.
Freeshuffle relies on the underlying logic of Isodistances, or places that are equidistant from a given point, something like this:
Basically, it plots the chosen start/end on a map, takes a desired distance as input, and does the following:
- Calls the Mapbox Isochrone API with the starting/ending coordinates and 1/3 of the desired distance as query parameters (1/3 was chosen as 2 other points [3 total] is the minimum # of points required to plot a circle-like shape)
- Draws the returned isochrone (isodistance in our case) around the Start/End point
- Chooses a random point along the isodistance as the 2nd stop on the route
- Calls the Mapbox Isochrone API again with the 2nd stop and 1/3 of desired distance as query params
- Paints another isodistance around it
- Finds a list of all intersections between these isodistances (using Turf.js)
- Picks whichever intersection is closest to 1/3 of the desired distance (straight-line distance, using Turf.js again)
- Plots all three points and calls the Mapbox Directions API to find a route between them
- Throws the result out if it doesn't fall within 0.5 miles/km of the desired distance and recurses
- Paints the route on the map
Visualization of the algorithm in action (please excuse the messy UI :) ):