7.0 KiB
7.0 KiB
Route Generation System - Quick Integration Guide
๐ Quick Start
1. Add OpenAI API Key
Add your OpenAI API key to .env:
VITE_OPENAI_API_KEY=sk-your-api-key-here
2. Integrate MapTiler Map
Replace existing map component in TripPlanner:
import { MapTilerMap } from '@/components/TripPlanner/Map/MapTilerMap';
import { useRouteStore } from '@/store/route-store';
import { getAllCappadociaPOIs } from '@/services/poi-service';
// In your component
const { allPOIs, setAllPOIs } = useRouteStore();
// Load POIs on mount
useEffect(() => {
const loadPOIs = async () => {
const pois = await getAllCappadociaPOIs();
setAllPOIs(pois);
};
loadPOIs();
}, []);
// Render map
<MapTilerMap
places={tripPlaces}
allPOIs={allPOIs}
center={[38.6431, 34.8289]}
zoom={11}
/>
3. Add Route Generator Button
import { RouteGeneratorWizard } from '@/components/TripPlanner/RouteGenerator';
import { useState } from 'react';
const [showRouteGenerator, setShowRouteGenerator] = useState(false);
// Button
<Button
onClick={() => setShowRouteGenerator(true)}
className="fixed bottom-6 right-6 rounded-full shadow-lg"
size="lg"
>
<Sparkles className="mr-2 h-5 w-5" />
Generate Personalized Route
</Button>
// Wizard
<RouteGeneratorWizard
open={showRouteGenerator}
onClose={() => setShowRouteGenerator(false)}
onComplete={(route) => {
// Add route places to trip
route.days.forEach((day, dayIndex) => {
day.places.forEach((place) => {
// Add place to trip day
addPlaceToTripDay(dayIndex + 1, place);
});
});
setShowRouteGenerator(false);
toast.success('Route added to your trip!');
}}
/>
๐ฆ Available Components
MapTilerMap
<MapTilerMap
places={Place[]} // Places in route
allPOIs={POI[]} // All available POIs
center={[lat, lng]} // Map center (optional)
zoom={number} // Zoom level (optional)
className={string} // CSS class (optional)
/>
RouteGeneratorWizard
<RouteGeneratorWizard
open={boolean} // Show/hide wizard
onClose={() => void} // Close handler
onComplete={(route: RouteRecommendation) => void} // Success handler
/>
๐ง Utility Functions
Route Optimizer
import { optimizeRoute, calculateTotalDistance, getRouteStatistics } from '@/utils/route-optimizer';
// Optimize places by distance
const optimized = optimizeRoute(places, {
maxDurationMinutes: 480, // 8 hours
priorityWeight: 0.5
});
// Calculate distance
const distance = calculateTotalDistance(places);
// Get statistics
const stats = getRouteStatistics(places);
// Returns: { totalDistance, totalTravelTime, totalActivityTime, totalTime }
Map Interactions
import { addPOIToRoute, removePlaceFromRoute, isPlaceInRoute } from '@/utils/map-interactions';
// Add POI to route
await addPOIToRoute(poiId);
// Remove place from route
removePlaceFromRoute(placeId);
// Check if place is in route
const inRoute = isPlaceInRoute(placeId);
POI Service
import { getAllCappadociaPOIs, getPOIById, getPOIsByCategory, searchPOIs } from '@/services/poi-service';
// Get all POIs
const allPOIs = await getAllCappadociaPOIs();
// Get single POI
const poi = await getPOIById(id);
// Get by category
const restaurants = await getPOIsByCategory('restaurant');
// Search
const results = await searchPOIs('Gรถreme');
๐จ Customization
Change Map Style
Edit MapTilerMap.tsx:
const MAPTILER_STYLE = 'outdoor-v2'; // or 'streets-v2', 'satellite', etc.
Modify Interests
Edit PreferencesStep.tsx:
const INTERESTS = [
{ id: 'custom', label: 'Custom Interest', icon: '๐ฏ' },
// Add more interests
];
Adjust Rate Limits
Edit api-rate-limiter.ts:
export const openaiRateLimiter = new RateLimiter({
maxRequests: 20, // Increase limit
windowMs: 60 * 60 * 1000, // 1 hour
});
Customize Marker Icons
Edit MapTilerMap.tsx getCategoryIcon() function:
const iconConfig = {
restaurant: { color: '#FF6347', icon: '๐ฝ๏ธ' },
custom: { color: '#YOUR_COLOR', icon: '๐ฏ' },
// Add more categories
};
๐ Security
Rate Limiting
- OpenAI: 10 requests/hour per user
- MapTiler: 100 requests/minute per user
- Automatic cleanup of expired entries
API Key Protection
- OpenAI key stored in environment variables
- Never exposed to client (use Edge Functions for production)
- Rate limiter prevents abuse
๐ Troubleshooting
Map Not Loading
- Check MapTiler API key in
.env - Verify internet connection
- Check browser console for errors
- Ensure Leaflet CSS is imported
Route Generation Fails
- Check OpenAI API key is set
- Verify API key has credits
- Check rate limit not exceeded
- Review browser console for errors
POIs Not Showing
- Verify places exist in database
- Check POI service returns data
- Ensure
setAllPOIs()is called - Check map bounds include POIs
Markers Not Clickable
- Verify
map-interactions.tsis imported inmain.tsx - Check global functions are defined
- Ensure popup HTML is correct
- Test on different browsers
๐ Monitoring
Check API Usage
import { openaiRateLimiter } from '@/utils/api-rate-limiter';
// Get remaining requests
const remaining = openaiRateLimiter.getRemainingRequests(userId);
// Get reset time
const resetTime = openaiRateLimiter.getResetTime(userId);
Database Queries
-- Check generated routes
SELECT * FROM generated_routes WHERE user_id = 'user-id';
-- Check API usage
SELECT * FROM api_usage WHERE user_id = 'user-id' ORDER BY created_at DESC;
๐ฏ Best Practices
- Load POIs Once: Load POIs on component mount, not on every render
- Debounce Map Updates: Avoid updating map too frequently
- Cache Routes: Store generated routes in database
- Error Boundaries: Wrap components in error boundaries
- Loading States: Show loading indicators during API calls
- User Feedback: Use toast notifications for actions
- Mobile First: Test on mobile devices
- Accessibility: Ensure keyboard navigation works
๐ Resources
๐ Support
For issues or questions:
- Check implementation summary:
ROUTE_GENERATION_IMPLEMENTATION.md - Review component source code
- Check browser console for errors
- Test with sample data
- Verify environment variables
โ Checklist
Before deploying:
- OpenAI API key configured
- MapTiler API key verified
- Database migrations applied
- POIs loaded successfully
- Map renders correctly
- Route generation works
- Rate limiting active
- Error handling tested
- Mobile responsive
- Loading states working
- Toast notifications showing
- Lint checks passed