Speed TTS Warning
This example demonstrates how to integrate Text-to-Speech (TTS) voice warnings for speed limit changes during navigation using the Maps SDK for TypeScript with browser TTS capabilities.
Live Demo
Overview
The example showcases the following features:
- Route calculation between two points
- Navigation simulation with position following
- Speed limit detection using
AlarmService - Real-time TTS announcements for speed limit changes
- Visual display of current speed limits
- Integration with browser's
window.speechSynthesisAPI
Code Implementation
Initialize GemKit and setup UI
TTS Engine Implementation
Calculate Route
Start Simulation with Speed Alarms
Stop Simulation
Visual Speed Display
Key Features
- Browser TTS Integration: Uses
window.speechSynthesisfor voice announcements - Speed Limit Detection: Real-time monitoring via
AlarmService.onSpeedLimit - Automatic Conversion: Converts speed from m/s to km/h for display
- Change Detection: Only announces when speed limit changes
- Visual Feedback: Bottom panel displays current speed limit
- Position Following: Camera follows simulated position during navigation
- Clean State Management: Proper cleanup when stopping simulation
Alarm Service
The AlarmService provides real-time notifications during navigation:
alarmListener = AlarmListener.create({
onSpeedLimit: async (speed: number, limit: number, insideCityArea: boolean) => {
// speed: Current vehicle speed (m/s)
// limit: Speed limit at current location (m/s)
// insideCityArea: Whether inside city boundaries
}
});
alarmService = AlarmService.create(alarmListener);
TTS Configuration
Customize voice characteristics through SpeechSynthesisUtterance:
utterance.rate = 0.75; // Speed: 0.1 to 10 (1 = normal)
utterance.pitch = 1.0; // Pitch: 0 to 2 (1 = normal)
utterance.volume = 0.8; // Volume: 0 to 1 (1 = maximum)
utterance.lang = 'en-US'; // Language code
Speed Conversion
Convert between m/s (SDK format) and km/h (display format):
// m/s to km/h
const speedKmh = Math.round(speedMs * 3.6);
// km/h to m/s
const speedMs = speedKmh / 3.6;
Workflow
- Build Route: Calculate route between departure and destination
- Start Simulation: Begin simulated navigation with alarm monitoring
- Speed Detection:
AlarmServicedetects speed limit changes - TTS Announcement: Browser TTS speaks the new speed limit
- Visual Update: Bottom panel displays current speed limit
- Stop Simulation: Clean up navigation and alarm services
Use Cases
Speed TTS warnings are useful for:
- Driver Assistance: Alert drivers to changing speed limits
- Navigation Safety: Prevent speeding violations
- Educational Tools: Teaching road rules and speed awareness
- Fleet Management: Monitor speed compliance during routes
- Accessibility: Audio feedback for visually impaired users
- Testing: Verify route speed limit data accuracy
Browser TTS Limitations
When using window.speechSynthesis:
- Voice quality varies by browser and OS
- Limited voice selection compared to native platforms
- No human voice support (TTS only)
- Utterances may be interrupted by page events
- Some browsers require user interaction before TTS
Best Practices
- Cancel Before Speaking: Always cancel ongoing speech before new announcements
- Debounce Changes: Only announce when speed limit actually changes
- Clear Speech: Use slightly slower rate (0.75) for better clarity
- Error Handling: Check for
synth.speakingbefore canceling - Cleanup: Stop TTS when navigation ends
Next Steps
- Voice Guidance - Complete voice guidance documentation
- Navigate Route - Real navigation implementation
- Simulate Navigation - Basic simulation without TTS