| 1 | import 'dart:math'; |
| 2 | import 'package:openapi/openapi.dart'; |
| 3 | import 'package:flutter/material.dart'; |
| 4 | import 'package:flutter_map/flutter_map.dart'; |
| 5 | import 'package:flutter_map_animations/flutter_map_animations.dart'; |
| 6 | import 'package:latlong2/latlong.dart'; |
| 7 | import 'package:aero/constants/map.dart'; |
| 8 | |
| 9 | MapBounds getVisibleMapBounds(AnimatedMapController |
| 10 | final mapState = mapController.mapController. |
| 11 | final center = mapState.center; |
| 12 | final zoom = mapState.zoom; |
| 13 | |
| 14 | // Calculate the visible bounds based on zoom level |
| 15 | // At zoom level 1, the world is roughly 180 degrees wide |
| 16 | // Each zoom level doubles the detail (halves the visible area) |
| 17 | final degreesPerPixel = 360 / |
| 18 | final screenWidth = 400; |
| 19 | final screenHeight = 400; |
| 20 | |
| 21 | final latSpan = (screenHeight * degreesPerPixel) / |
| 22 | final lngSpan = (screenWidth * degreesPerPixel) / |
| 23 | |
| 24 | // Calculate bounds |
| 25 | double minLat = center.latitude - |
| 26 | double maxLat = center.latitude + |
| 27 | double minLng = center.longitude - |
| 28 | double maxLng = center.longitude + |
| 29 | |
| 30 | // Apply reasonable bounds to prevent queries that are too large |
| 31 | // Maximum span: 30 degrees (roughly 3000km at equator) |
| 32 | const maxSpan = 30.0; |
| 33 | |
| 34 | if (maxLat - minLat > maxSpan) { |
| 35 | final centerLat = (maxLat + minLat) / |
| 36 | minLat = centerLat - maxSpan / 2; |
| 37 | maxLat = centerLat + maxSpan / 2; |
| 38 | } |
| 39 | |
| 40 | if (maxLng - minLng > maxSpan) { |
| 41 | final centerLng = (maxLng + minLng) / |
| 42 | minLng = centerLng - maxSpan / 2; |
| 43 | maxLng = centerLng + maxSpan / 2; |
| 44 | } |
| 45 | |
| 46 | // Ensure bounds are within world limits |
| 47 | minLat = minLat.clamp(-90.0 |
| 48 | maxLat = maxLat.clamp(-90.0 |
| 49 | minLng = minLng.clamp(-180.0 |
| 50 | maxLng = maxLng.clamp(-180.0 |
| 51 | |
| 52 | return MapBounds( |
| 53 | minLat: minLat, |
| 54 | maxLat: maxLat, |
| 55 | minLng: minLng, |
| 56 | maxLng: maxLng, |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | class MapBounds { |
| 61 | final double minLat; |
| 62 | final double maxLat; |
| 63 | final double minLng; |
| 64 | final double maxLng; |
| 65 | |
| 66 | const MapBounds({ |
| 67 | required this.minLat, |
| 68 | required this.maxLat, |
| 69 | required this.minLng, |
| 70 | required this.maxLng, |
| 71 | }); |
| 72 | |
| 73 | @override |
| 74 | String toString() { |
| 75 | return 'MapBounds(minLat: $minLat, maxLat: |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | class FlightMap extends StatefulWidget |
| 80 | final AnimatedMapController mapController; |
| 81 | final AirportEntity? departureAirport; |
| 82 | final AirportEntity? arrivalAirport; |
| 83 | final List<LatLng |
| 84 | final List<LatLng |
| 85 | final double? currentHeading; |
| 86 | final Function(FlightsControllerGetFlightsInBoundsV1200ResponseFlightsInner |
| 87 | onAircraftSelected; |
| 88 | final bool isFlightInAir; |
| 89 | |
| 90 | const FlightMap({ |
| 91 | super.key, |
| 92 | required this.mapController, |
| 93 | this.arrivalAirport, |
| 94 | this.departureAirport, |
| 95 | required this.coordinates, |
| 96 | this.flightTrackPoints = const |
| 97 | this.currentHeading, |
| 98 | this.onAircraftSelected, |
| 99 | this.isFlightInAir = false, |
| 100 | }); |
| 101 | |
| 102 | @override |
| 103 | State<FlightMap> createState |
| 104 | } |
| 105 | |
| 106 | class _FlightMapState extends State |
| 107 | @override |
| 108 | void initState() { |
| 109 | super.initState(); |
| 110 | } |
| 111 | |
| 112 | @override |
| 113 | void dispose() { |
| 114 | super.dispose(); |
| 115 | } |
| 116 | |
| 117 | @override |
| 118 | Widget build(BuildContext |
| 119 | return Stack( |
| 120 | children: [ |
| 121 | FlutterMap( |
| 122 | mapController: widget.mapController.mapController, |
| 123 | options: MapOptions( |
| 124 | initialCenter: |
| 125 | const LatLng(28.086815545374254, |
| 126 | initialZoom: 5, |
| 127 | cameraConstraint: CameraConstraint.containLatitude()) |
| 128 | children: [ |
| 129 | openStreetMapTileLayer, |
| 130 | MarkerLayer( |
| 131 | markers: [ |
| 132 | if (widget.arrivalAirport != null) |
| 133 | Marker( |
| 134 | point: LatLng(double. |
| 135 | double.parse(widget.arrivalAirport |
| 136 | width: 18, |
| 137 | height: 18, |
| 138 | child: Container( |
| 139 | decoration: BoxDecoration( |
| 140 | color: Colors.redAccent, |
| 141 | shape: BoxShape.circle, |
| 142 | border: Border.all(color |
| 143 | boxShadow: [ |
| 144 | BoxShadow( |
| 145 | color: Colors.black.withOpacity |
| 146 | blurRadius: 4, |
| 147 | ), |
| 148 | ], |
| 149 | ), |
| 150 | ), |
| 151 | ), |
| 152 | if (widget.departureAirport != null) |
| 153 | Marker( |
| 154 | point: LatLng(double. |
| 155 | double.parse(widget.departureAirport |
| 156 | width: 18, |
| 157 | height: 18, |
| 158 | child: Container( |
| 159 | decoration: BoxDecoration( |
| 160 | color: Colors.green, |
| 161 | shape: BoxShape.circle, |
| 162 | border: Border.all(color |
| 163 | boxShadow: [ |
| 164 | BoxShadow( |
| 165 | color: Colors.black.withOpacity |
| 166 | blurRadius: 4, |
| 167 | ), |
| 168 | ], |
| 169 | ), |
| 170 | ), |
| 171 | ), |
| 172 | ], |
| 173 | ), |
| 174 | if (widget.arrivalAirport != null && |
| 175 | widget.departureAirport != null && |
| 176 | widget.flightTrackPoints.isEmpty) |
| 177 | PolylineLayer( |
| 178 | polylines: [ |
| 179 | Polyline( |
| 180 | points: [ |
| 181 | LatLng( |
| 182 | double.parse(widget.arrivalAirport |
| 183 | double.parse(widget.arrivalAirport |
| 184 | ), |
| 185 | LatLng( |
| 186 | double.parse(widget.departureAirport |
| 187 | double.parse(widget.departureAirport |
| 188 | ), |
| 189 | ], |
| 190 | color: Colors.blueAccent, |
| 191 | strokeWidth: 4, |
| 192 | borderStrokeWidth: 2, |
| 193 | borderColor: Colors.white70, |
| 194 | ), |
| 195 | ], |
| 196 | ), |
| 197 | if (widget.flightTrackPoints.isEmpty && |
| 198 | widget.coordinates.isNotEmpty) |
| 199 | PolylineLayer( |
| 200 | polylines: [ |
| 201 | Polyline( |
| 202 | points: widget.coordinates.map((e) |
| 203 | color: Colors.blueAccent, |
| 204 | strokeWidth: 4, |
| 205 | borderStrokeWidth: 2, |
| 206 | borderColor: Colors.white70, |
| 207 | pattern: StrokePattern.dashed(segments |
| 208 | ], |
| 209 | ), |
| 210 | if (widget.flightTrackPoints.isNotEmpty) |
| 211 | PolylineLayer( |
| 212 | drawInSingleWorld: false, |
| 213 | polylines: [ |
| 214 | Polyline( |
| 215 | points: widget.flightTrackPoints, |
| 216 | color: Colors.green, |
| 217 | strokeWidth: 3, |
| 218 | borderStrokeWidth: 1, |
| 219 | borderColor: Colors.white70, |
| 220 | strokeCap: StrokeCap.round, |
| 221 | strokeJoin: StrokeJoin.round, |
| 222 | ), |
| 223 | ], |
| 224 | ), |
| 225 | // Show moving aircraft marker only when airborne |
| 226 | if (widget.flightTrackPoints.isNotEmpty && widget. |
| 227 | MarkerLayer( |
| 228 | markers: [ |
| 229 | Marker( |
| 230 | point: widget.flightTrackPoints.last, |
| 231 | width: 30, |
| 232 | height: 30, |
| 233 | child: Transform.rotate( |
| 234 | angle: widget.currentHeading != null |
| 235 | ? (widget.currentHeading! * pi / |
| 236 | : 0, |
| 237 | child: Container( |
| 238 | decoration: BoxDecoration( |
| 239 | color: Colors.green, |
| 240 | shape: BoxShape.circle, |
| 241 | border: Border.all( |
| 242 | color: Colors.white, |
| 243 | width: 2, |
| 244 | ), |
| 245 | ), |
| 246 | child: Icon( |
| 247 | Icons.flight, |
| 248 | color: Colors.white, |
| 249 | size: 16, |
| 250 | ), |
| 251 | ), |
| 252 | ), |
| 253 | ), |
| 254 | ], |
| 255 | ), |
| 256 | // When not airborne, show static origin/destination markers from track points |
| 257 | if (widget.flightTrackPoints.isNotEmpty && ! |
| 258 | MarkerLayer( |
| 259 | markers: [ |
| 260 | Marker( |
| 261 | point: widget.flightTrackPoints.first, |
| 262 | width: 18, |
| 263 | height: 18, |
| 264 | child: Container( |
| 265 | decoration: BoxDecoration( |
| 266 | color: Colors.green, |
| 267 | shape: BoxShape.circle, |
| 268 | border: Border.all(color |
| 269 | boxShadow: [ |
| 270 | BoxShadow( |
| 271 | color: Colors.black.withOpacity |
| 272 | blurRadius: 4, |
| 273 | ), |
| 274 | ], |
| 275 | ), |
| 276 | ), |
| 277 | ), |
| 278 | Marker( |
| 279 | point: widget.flightTrackPoints.last, |
| 280 | width: 18, |
| 281 | height: 18, |
| 282 | child: Container( |
| 283 | decoration: BoxDecoration( |
| 284 | color: Colors.redAccent, |
| 285 | shape: BoxShape.circle, |
| 286 | border: Border.all(color |
| 287 | boxShadow: [ |
| 288 | BoxShadow( |
| 289 | color: Colors.black.withOpacity |
| 290 | blurRadius: 4, |
| 291 | ), |
| 292 | ], |
| 293 | ), |
| 294 | ), |
| 295 | ), |
| 296 | ], |
| 297 | ), |
| 298 | ], |
| 299 | ), |
| 300 | ], |
| 301 | ); |
| 302 | } |
| 303 | } |