Skip to content

Mapbox Setup Guide πŸ—ΊοΈ

onice edited this page Jul 5, 2025 · 1 revision

Mapbox Setup Guide πŸ—ΊοΈ

Complete guide to setting up Mapbox for interactive maps and cost-effective static map caching.

πŸ“‹ Table of Contents

Overview

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!

Create Mapbox Account

Step 1: Sign Up

  1. Visit Mapbox: https://account.mapbox.com/auth/signup/
  2. Create account with email or GitHub
  3. Verify your email address
  4. Complete profile setup

Step 2: Understand Pricing

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

Frontend Maps Setup

Step 1: Create Public Token

  1. Go to Access Tokens: https://account.mapbox.com/access-tokens/
  2. Copy your "Default public token" or create a new one
  3. Configure URL restrictions (recommended for security):
    https://your-project.vercel.app/*
    https://*.vercel.app/*
    http://localhost:3000/*
    

Step 2: Add to Vercel Environment

  1. Go to Vercel Dashboard β†’ Your project
  2. Settings β†’ Environment Variables
  3. Add new variable:
    Name: NEXT_PUBLIC_MAPBOX_TOKEN
    Value: pk.eyJ1IjoiZXhhbXBsZSIsImEiOiJjbGV4YW1wbGUifQ.example
    
  4. Redeploy your application

Step 3: Test Frontend Maps

Visit your site and check:

  • βœ… Dashboard: Maps should appear for GPS activities
  • βœ… Activities page: Route maps display correctly
  • βœ… Interactive features: Zoom, pan work properly

Static Map Caching Setup

Step 1: Create GitHub Actions Token

⚠️ Critical: This token must have NO URL restrictions for GitHub Actions to work.

  1. Create new token: https://account.mapbox.com/access-tokens/
  2. Token settings:
    Name: GitHub-Actions-Static-Maps
    URL restrictions: (leave completely empty)
    Scopes: Default (all enabled)
    
  3. Save the token

Step 2: Add GitHub Secret

  1. Go to your repository β†’ Settings β†’ Secrets and variables β†’ Actions
  2. Add new secret:
    Name: MAPBOX_TOKEN
    Value: pk.eyJ1IjoiZXhhbXBsZSIsImEiOiJjbGV4YW1wbGUifQ.no-restrictions
    

Step 3: Test Token Configuration

  1. Go to Actions β†’ "Test Mapbox Configuration"
  2. Run workflow β†’ "basic" test
  3. Check results:
    • βœ… Token validation passes
    • βœ… Static map generation works
    • βœ… Test map artifact created

Cost Optimization

Understanding the Savings

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

Automatic Cache Management

The system automatically:

  • βœ… Generates maps for new activities
  • βœ… Skips existing maps to avoid duplicate costs
  • βœ… Cleans up orphaned maps
  • βœ… Monitors usage and provides statistics

Monitor Your Usage

  1. Mapbox Dashboard: https://account.mapbox.com/
  2. Check "Usage" section regularly
  3. Set up billing alerts for peace of mind
  4. Review cache statistics: /api/cache/stats

Testing and Verification

Test Static Map Generation

  1. Manual trigger:

    # In your repository Actions
    Workflow: "Sync Strava Data"
    Options: βœ… "Regenerate all static maps"
  2. Check results:

    • Maps appear in /public/maps/ directory
    • File names match Strava activity IDs
    • File sizes are reasonable (50-200KB each)

Verify Cache Usage

  1. Visit test page: https://your-site.com/test-maps
  2. Check cache statistics: https://your-site.com/api/cache/stats
  3. Browser dev tools:
    • Network tab should show /maps/12345.png requests
    • NOT api.mapbox.com requests for cached activities

Performance Testing

Before caching (first visit):

  • Map loading: 500-2000ms
  • Multiple API calls visible

After caching (subsequent visits):

  • Map loading: 50-100ms
  • Static file requests only

Troubleshooting

Common Issues

1. Maps Not Loading

Error: Failed to load map

Solutions:

  • βœ… Check NEXT_PUBLIC_MAPBOX_TOKEN is set
  • βœ… Verify token has correct URL restrictions
  • βœ… Check browser console for errors

2. Static Maps Not Generating

Error: 403 Forbidden

Solutions:

  • βœ… Ensure GitHub Actions token has NO URL restrictions
  • βœ… Check MAPBOX_TOKEN secret is set correctly
  • βœ… Run the test workflow to verify

3. High API Usage

Warning: Approaching usage limits

Solutions:

  • βœ… Verify static caching is working
  • βœ… Check cache hit rates in statistics
  • βœ… Look for API calls in browser network tab

4. Token Errors

Error: Invalid token

Solutions:

  • βœ… Regenerate tokens if expired
  • βœ… Check for typos in environment variables
  • βœ… Verify token permissions and scopes

Debug Commands

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"

GitHub Actions Debugging

  1. Check workflow logs:

    • Actions β†’ Latest run β†’ "Generate static maps"
    • Look for error messages and API responses
  2. Common log messages:

    βœ… "Generated map for activity 12345"
    ⏭️ "Map for activity 12345 already exists"
    ❌ "Could not generate URL for activity 12345"
    

Advanced Configuration

Custom Map Styles

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-v9

Map Size Optimization

Adjust dimensions in generation script:

# Smaller files, faster loading
width, height = 300, 200  # Default: 400, 300

# Higher quality, larger files
width, height = 600, 400

Batch Processing

For large activity counts:

# Add rate limiting
time.sleep(0.2)  # 200ms between requests

# Process in batches
batch_size = 50

πŸŽ‰ Success!

Your 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:

Clone this wiki locally