-
Notifications
You must be signed in to change notification settings - Fork 0
Mapbox Setup Guide πΊοΈ
Complete guide to setting up Mapbox for interactive maps and cost-effective static map caching.
- Overview
- Create Mapbox Account
- Frontend Maps Setup
- Static Map Caching Setup
- Cost Optimization
- Testing and Verification
- Troubleshooting
Mapbox integration provides:
- πΊοΈ Interactive route maps on your running page
- π Activity location visualization with markers
- π° Static map caching for 99% cost reduction
- β‘ Fast loading with pre-generated map images
- π¨ Customizable map styles and themes
Cost Impact: With static caching, you'll use ~$5 one-time vs $100s monthly!
- Visit Mapbox: https://account.mapbox.com/auth/signup/
- Create account with email or GitHub
- Verify your email address
- Complete profile setup
Free Tier Includes:
- 50,000 map loads per month
- 50,000 static map requests per month
- Perfect for personal running pages
With Static Caching:
- One-time generation cost only
- Virtually unlimited usage after caching
- 99%+ cost reduction
- Go to Access Tokens: https://account.mapbox.com/access-tokens/
- Copy your "Default public token" or create a new one
-
Configure URL restrictions (recommended for security):
https://your-project.vercel.app/* https://*.vercel.app/* http://localhost:3000/*
- Go to Vercel Dashboard β Your project
- Settings β Environment Variables
-
Add new variable:
Name: NEXT_PUBLIC_MAPBOX_TOKEN Value: pk.eyJ1IjoiZXhhbXBsZSIsImEiOiJjbGV4YW1wbGUifQ.example - Redeploy your application
Visit your site and check:
- β Dashboard: Maps should appear for GPS activities
- β Activities page: Route maps display correctly
- β Interactive features: Zoom, pan work properly
- Create new token: https://account.mapbox.com/access-tokens/
-
Token settings:
Name: GitHub-Actions-Static-Maps URL restrictions: (leave completely empty) Scopes: Default (all enabled) - Save the token
- Go to your repository β Settings β Secrets and variables β Actions
-
Add new secret:
Name: MAPBOX_TOKEN Value: pk.eyJ1IjoiZXhhbXBsZSIsImEiOiJjbGV4YW1wbGUifQ.no-restrictions
- Go to Actions β "Test Mapbox Configuration"
- Run workflow β "basic" test
-
Check results:
- β Token validation passes
- β Static map generation works
- β Test map artifact created
Without Static Caching:
- Every page visit = API calls
- 500 activities Γ 10 views/day = 5,000 API calls/day
- Monthly cost: $50-200+
With Static Caching:
- One-time generation: 500 API calls total
- Daily usage: 0 API calls (static files)
- Monthly cost: ~$0.50 one-time
The system automatically:
- β Generates maps for new activities
- β Skips existing maps to avoid duplicate costs
- β Cleans up orphaned maps
- β Monitors usage and provides statistics
- Mapbox Dashboard: https://account.mapbox.com/
- Check "Usage" section regularly
- Set up billing alerts for peace of mind
-
Review cache statistics:
/api/cache/stats
-
Manual trigger:
# In your repository Actions Workflow: "Sync Strava Data" Options: β "Regenerate all static maps"
-
Check results:
- Maps appear in
/public/maps/directory - File names match Strava activity IDs
- File sizes are reasonable (50-200KB each)
- Maps appear in
-
Visit test page:
https://your-site.com/test-maps -
Check cache statistics:
https://your-site.com/api/cache/stats -
Browser dev tools:
- Network tab should show
/maps/12345.pngrequests - NOT
api.mapbox.comrequests for cached activities
- Network tab should show
Before caching (first visit):
- Map loading: 500-2000ms
- Multiple API calls visible
After caching (subsequent visits):
- Map loading: 50-100ms
- Static file requests only
Error: Failed to load map
Solutions:
- β
Check
NEXT_PUBLIC_MAPBOX_TOKENis set - β Verify token has correct URL restrictions
- β Check browser console for errors
Error: 403 Forbidden
Solutions:
- β Ensure GitHub Actions token has NO URL restrictions
- β
Check
MAPBOX_TOKENsecret is set correctly - β Run the test workflow to verify
Warning: Approaching usage limits
Solutions:
- β Verify static caching is working
- β Check cache hit rates in statistics
- β Look for API calls in browser network tab
Error: Invalid token
Solutions:
- β Regenerate tokens if expired
- β Check for typos in environment variables
- β Verify token permissions and scopes
Test token locally:
curl "https://api.mapbox.com/styles/v1/mapbox/streets-v11?access_token=YOUR_TOKEN"Check static map generation:
curl "https://api.mapbox.com/styles/v1/mapbox/dark-v11/static/pin-l+ff0000(-122.4194,37.7749)/-122.4194,37.7749,12,0/400x300@2x?access_token=YOUR_TOKEN"Monitor cache statistics:
curl "https://your-site.com/api/cache/stats"-
Check workflow logs:
- Actions β Latest run β "Generate static maps"
- Look for error messages and API responses
-
Common log messages:
β "Generated map for activity 12345" βοΈ "Map for activity 12345 already exists" β "Could not generate URL for activity 12345"
Edit scripts/generate-static-maps.py:
# Change map style
base_url = "https://api.mapbox.com/styles/v1/mapbox/satellite-v9/static/"
# Options: streets-v11, outdoors-v11, light-v10, dark-v10, satellite-v9Adjust dimensions in generation script:
# Smaller files, faster loading
width, height = 300, 200 # Default: 400, 300
# Higher quality, larger files
width, height = 600, 400For large activity counts:
# Add rate limiting
time.sleep(0.2) # 200ms between requests
# Process in batches
batch_size = 50Your Mapbox integration is now complete with:
- β Interactive maps on your website
- β Cost-effective static map caching
- β Automatic map generation for new activities
- β 99%+ reduction in ongoing API costs
Next Steps: