Earlier this year they angered many developers by unplugging their previous API, leaving many third-party apps stranded. My own interest is in seeing how cyclists use the city: which routes are preferred. Unfortunately, the data available in Strava’s v3 API is extremely limited. Most of the API is designed to reveal a selected user’s data, assuming they have specifically granted the program access. But there doesn’t seem to be a way to look at aggregate data.
In fact there seems to be only a single API function which accepts a geographic bounding box as an input. That API, the “segment explorer” returns up to 10 “popular” segments.
I wanted to see how easy it would be to use the API in a little test program. The result, my Strava Explorer, doesn’t really do anything interesting other than prove I can connect to the Strava API.
To start, you have to get a (free) Strava account, and then sign up for the Strava Developer API, which is quick and painless. Registration will give you your unique access token, which you’ll need as a parameter for the API calls. The Strava API Reference is kept on their Github site.
The API calls are handled with HTTP, so to call you just need to configure a URL, and then read the response. Parameters are plopped into the query string. You can easily test this by entering the API URL in the browser and seeing the JSON results displayed.
echo file_get_contents("https://www.strava.com/api/v3/segments/explore" .
"?bounds=" . $_GET["bounds"] . "&access_token=YOUR-TOKEN-HERE");
My map doesn’t use all of the information that I get back. I use the “id” field to track which segments have been drawn, since the user can pan and zoom the map and make more requests to show segments. I use the “points” field to draw the segment. This field uses compressed lat/lng coordinates that look like “}g|eFnm@n@Op@VJr@” – you can feed this to the Google Maps Geometry Library‘s decodePath function. decodePath() takes a string and returns an array you can use as the path of a Polyline object, which is then easily drawn on a Google map.
I use the StyledMarker library to draw markers that have the name of the segment inside. I use the “avg_grade” field to affect the color of the marker, so that steeper segments appear more purplish.