Skip to content

GreenRoute is a web application that optimizes daily commutes by recommending the most eco-friendly routes using real-time public transit, bike-sharing, and carbon emission data.

Notifications You must be signed in to change notification settings

demon2202/GreenRoute

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

91 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŒฟ GreenRoute - Eco-Friendly Route Planner

GreenRoute Banner

License: MIT React Vite Tailwind CSS Mapbox

GreenRoute is a modern, sustainable navigation web application designed to help users reduce their carbon footprint. Unlike traditional navigation apps that prioritize speed above all else, GreenRoute calculates and suggests the most environmentally friendly routes between two locations.

"The greatest threat to our planet is the belief that someone else will save it." โ€“ Robert Swan


๐Ÿ“– Table of Contents


๐ŸŒ The "Why" - A Deeper Look

The Invisible Cost of Speed

Modern navigation algorithms (Google Maps, Waze) are optimized for one single metric: Time. They will happily route you 5 miles further to save 1 minute of travel time.

However, Distance โ‰  Emissions.

  • Stop-and-go traffic emits 2-3x more COโ‚‚ than highway driving.
  • A slightly longer route at a steady speed is often greener than a shorter, congested one.
  • Taking a bus instead of a car reduces emissions by ~66%.

The Impact

Transportation accounts for 24% of direct COโ‚‚ emissions from fuel combustion. GreenRoute empowers users to visualize this impact.

Mode of Transport COโ‚‚ per Mile (Avg) Impact Level
๐Ÿš— SUV / Truck 360g ๐Ÿ”ด High
๐Ÿš— Sedan 192g ๐ŸŸ  Medium
๐ŸšŒ Public Transit 64g ๐ŸŸก Low
๐Ÿšฒ Cycling 0g ๐ŸŸข Zero
๐Ÿšถ Walking 0g ๐ŸŸข Zero

๐Ÿ—๏ธ System Architecture

GreenRoute follows a Client-Side Rendering (CSR) architecture powered by React and APIs.

graph TD
    A[User Interaction] -->|Inputs Locations| B(React Frontend)
    B -->|Geocoding Request| C[Mapbox Geocoding API]
    C -->|Coordinates| B
    B -->|Directions Request| D[Mapbox Directions API]
    D -->|Route Data (GeoJSON)| B
    B -->|Calculate Emissions| E[Utility Logic]
    E -->|Green Score| F[Zustand State Store]
    F -->|Update UI| G[Map & Info Cards]
Loading
  1. Input Layer: Users interact with the search bar using Mapbox Geocoder.
  2. Data Layer: We fetch routes for multiple profiles (driving, cycling, walking).
  3. Logic Layer: Raw distance/duration data is passed to emissions.ts to append carbon data.
  4. Presentation Layer: Mapbox GL JS renders the lines; React renders the UI overlay.

๐Ÿ“– Comprehensive User Guide

1. The Interface

The UI is split into two main layers:

  • Background: The interactive 3D map.
  • Foreground: The glass-morphism control panel (Desktop: Top-Left, Mobile: Bottom-Sheet).

2. How to Plan a "Green Trip"

  1. Grant Permissions: Allow Geolocation for the best experience.
  2. Set Origin: Defaults to your location. Click the "Current Location" icon to reset.
  3. Set Destination: Use the search bar. It supports "Fuzzy Search" (e.g., typing "coffee near me").
  4. Analyze Routes:
    • The Leaf Icon ๐Ÿƒ: Indicates the most eco-friendly route, even if it's not the fastest.
    • The Flame Icon ๐Ÿ”ฅ: Indicates high fuel consumption (avoid if possible).

3. Understanding the "Green Score"

The Green Score (0-100) is our proprietary efficiency rating.

  • 90-100: Excellent (Walking/Biking or very efficient driving).
  • 70-89: Good (Standard transit or direct driving).
  • < 50: Poor (Inefficient route, heavy traffic, or high-emission vehicle).

4. Offline Mode

The app caches the core assets using a Service Worker (via Vite PWA plugin). If you lose internet, you can still view the map tiles that were previously loaded, though routing APIs will be unavailable.


๐Ÿ’ป Code Deep Dive & Pattern Analysis

1. State Management (Zustand)

We use zustand for global state to avoid "Prop Drilling".

File: src/store/useRouteStore.ts

import create from 'zustand';

interface RouteState {
  origin: Coordinates | null;
  destination: Coordinates | null;
  routes: RouteData[];
  setRoutes: (routes: RouteData[]) => void;
}

export const useRouteStore = create<RouteState>((set) => ({
  origin: null,
  destination: null,
  routes: [],
  setRoutes: (routes) => set({ routes }),
}));

2. Custom Hook for Map Logic

To keep components clean, map logic is extracted into hooks.

File: src/hooks/useMapbox.ts

export const useMapbox = (containerRef: RefObject<HTMLDivElement>) => {
  useEffect(() => {
    if (!containerRef.current) return;
    
    const map = new mapboxgl.Map({
      container: containerRef.current,
      style: 'mapbox://styles/mapbox/streets-v11',
      center: [-74.5, 40],
      zoom: 9
    });

    return () => map.remove();
  }, []);
};

3. The Emission Algorithm

We calculate CO2 based on distance and estimated traffic drag (duration vs distance ratio).

File: src/utils/emissions.ts

export const calculateCO2 = (distance: number, duration: number, mode: string) => {
  const BASE_EMISSION = 120; // g/km for cars
  const TRAFFIC_PENALTY = 10; // g/min of delay

  let totalCO2 = (distance / 1000) * BASE_EMISSION;

  // If speed is below 20km/h (traffic), add penalty
  const speed = (distance / 1000) / (duration / 3600);
  if (mode === 'driving' && speed < 20) {
    totalCO2 += (duration / 60) * TRAFFIC_PENALTY;
  }

  return Math.round(totalCO2);
};

โœจ Key Features

Feature Description Status
๐ŸŒฑ Eco-Routing Prioritizes lower emissions using our custom algorithm. โœ… Live
๐Ÿšฒ Multi-Mode Compare Car, Bus, Bike, and Walking in one view. โœ… Live
๐Ÿ“Š Green Score Visual 0-100 efficiency rating. โœ… Live
๐ŸŒ™ Dark Mode Auto-adapts to system preferences. โœ… Live
๐Ÿ” Auth & Cloud Save routes and history via Firebase. โœ… Live
๐Ÿ’พ PWA Installable on iOS/Android. ๐Ÿšง Beta
๐Ÿ”Œ EV Stations Find charging spots along the route. ๐Ÿ”œ Coming Soon

๐Ÿ“ธ Screenshots

Landing Page Route Calculation Dark Mode
Landing Route Dark Mode

๐Ÿ›  Tech Stack

Frontend

  • Framework: React 18 (TypeScript)
  • Build Tool: Vite (Fast HMR)
  • Styling: Tailwind CSS + Headless UI (Accessible components)
  • Icons: Heroicons / Phosphor Icons

Maps & Data

  • Core Map: Mapbox GL JS
  • Routing: Mapbox Directions API
  • Geocoding: Mapbox Geocoding API

Backend (Serverless)

  • Auth: Firebase Authentication (Google/Email)
  • Database: Cloud Firestore (NoSQL)
  • Hosting: Netlify / Vercel

๐Ÿš€ Getting Started

Prerequisites

  • Node.js v16+
  • Mapbox Account (for API Token)

Installation

  1. Clone & Install

    git clone https://github.com/demon2202/GreenRoute.git
    cd GreenRoute
    npm install
  2. Configure Environment Create a .env.local file in the root:

    VITE_MAPBOX_TOKEN=pk.eyJ1IjoiZ... (Your token here)
    VITE_FIREBASE_API_KEY=AIzaSy...
    VITE_FIREBASE_AUTH_DOMAIN=greenroute.firebaseapp.com
    VITE_FIREBASE_PROJECT_ID=greenroute
  3. Run Development Server

    npm run dev

    Visit http://localhost:5173.


๐Ÿ“‚ Folder Structure

GreenRoute/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ assets/          # Static images/fonts
โ”‚   โ”œโ”€โ”€ components/
โ”‚   โ”‚   โ”œโ”€โ”€ layout/      # Header, Sidebar, Footer
โ”‚   โ”‚   โ”œโ”€โ”€ map/         # MapCanvas, Markers, Popups
โ”‚   โ”‚   โ”œโ”€โ”€ routing/     # RouteCard, TransportSelector
โ”‚   โ”‚   โ””โ”€โ”€ ui/          # Buttons, Inputs, Modals
โ”‚   โ”œโ”€โ”€ context/         # React Context providers
โ”‚   โ”œโ”€โ”€ hooks/           # Custom hooks (useAuth, useGeo)
โ”‚   โ”œโ”€โ”€ pages/           # Dashboard, Login, Settings
โ”‚   โ”œโ”€โ”€ services/        # API wrapper functions
โ”‚   โ”œโ”€โ”€ styles/          # Global CSS/Tailwind directives
โ”‚   โ”œโ”€โ”€ utils/           # Helpers (formatters, calculators)
โ”‚   โ””โ”€โ”€ App.tsx
โ””โ”€โ”€ ...config files

๐Ÿšข Deployment

Vercel (Recommended)

  1. Install Vercel CLI: npm i -g vercel
  2. Run vercel
  3. Set your Environment Variables in the Vercel Dashboard.

Netlify

  1. Drag and drop the dist folder (created after npm run build) to Netlify Drop.
  2. Or connect GitHub repo for auto-deploys.

๐Ÿ”ฎ Roadmap

  • Basic Eco-Routing
  • User Authentication
  • Gamification: Leaderboards for CO2 saved.
  • EV Support: Show charging stations.
  • Carpooling: Match with users on similar routes.
  • Weather: Alert if rain affects cycling routes.

๐Ÿ”ง Troubleshooting

Map is black/blank:

  • Check if your Mapbox Token is valid.
  • Ensure the style URL in MapCanvas.tsx is correct.

"Process is not defined":

  • Since we use Vite, use import.meta.env.VITE_VAR instead of process.env.REACT_APP_VAR.

๐Ÿค Contributing

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐Ÿ“„ License

Distributed under the MIT License. See LICENSE for more information.


Made with ๐Ÿ’š by demon2202

About

GreenRoute is a web application that optimizes daily commutes by recommending the most eco-friendly routes using real-time public transit, bike-sharing, and carbon emission data.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published