Overview
FlightsMapComponent provides an interactive map visualization using Leaflet.js to display real-time aircraft positions. The component handles map initialization, marker management, base layer switching, and user interactions.
Location: src/app/features/flights/flights-map/flights-map.component.ts
Key Features
- Leaflet Integration: Interactive map with zoom and pan controls
- Dynamic Markers: Rotating aircraft icons that update with flight positions
- Base Layer Switching: Toggle between street and satellite map views
- Selection Handling: Visual feedback for selected flights
- Reactive Updates: Automatic marker updates when flight data changes
Inputs
Array of flight objects to display on the map. Each flight must include latitude, longitude, and heading for proper marker placement and rotation.Flight Interface:
Component Configuration
Change Detection Strategy
UsesOnPush for optimized performance with reactive inputs.
View Encapsulation
UsesNone to allow Leaflet CSS to properly style map elements.
Map Initialization
The map is initialized after the view is ready:Map Configuration
- Initial Center:
[40, 0](latitude, longitude) - Initial Zoom: Level 6
- Zoom Controls: Repositioned to top-left and moved to custom host element
- Default Layer: Streets view
Marker Management
The component uses a dedicatedMapMarkerService to handle marker lifecycle:
Marker Updates
Two reactive effects handle marker updates:-
Position Updates: When flight positions change
- Adds new markers for new flights
- Removes markers for flights no longer in the array
- Updates positions for existing flights
-
Icon Updates: When flight selection changes
- Highlights selected flight marker
- Resets previously selected marker
Base Layer Switching
The component supports multiple map base layers:Available Base Layers
Base layers are defined inflights-map.layers.ts:
Selection Handling
Users can clear the current flight selection:Lifecycle Management
AfterViewInit
OnDestroy
Dependency Injection
- MapMarkerService: Handles marker creation, updates, and icon management
- FlightsStoreService: Provides selected flight state
Template Integration
The component template includes:Usage Example
Use the component in your template with flight data:Leaflet Plugin
The component uses theleaflet-moving-rotated-marker plugin for smooth marker animations:
- Smooth marker movement between positions
- Rotation to match aircraft heading
- Animated transitions
Performance Considerations
OnPush Change Detection
The component usesOnPush change detection strategy. Inputs must be immutable or use signals for change detection to work properly.
Marker Pooling
TheMapMarkerService reuses marker instances when possible to avoid creating/destroying DOM elements unnecessarily.
Effect Batching
Angular effects automatically batch updates, preventing redundant marker updates when multiple signals change simultaneously.Common Issues
Map Not Displaying
Ensure Leaflet CSS is imported in your styles:Markers Not Rotating
Verify that:- The
leaflet-moving-rotated-markerplugin is imported - Flight objects have valid
headingvalues (0-360) - The MapMarkerService is properly configured
Zoom Controls Not Appearing
Check that:- The
#zoomHostelement exists in the template - The element has appropriate CSS styling
- The zoom control initialization runs after view initialization
Related Components
- FlightsShellComponent - Parent orchestration component
- FlightsListComponent - Companion list view
Services Used
- MapMarkerService - Marker lifecycle management
- FlightsStoreService - Flight data and selection state
External Dependencies
- Leaflet - Core mapping library
- leaflet-moving-rotated-marker - Animated rotating markers
- @angular/material/button-toggle - Base layer switcher UI
