isBoundsCovered method

bool isBoundsCovered(
  1. LatLngBounds bounds
)

Check if the given bounds are mostly covered by cached data.

Implementation

bool isBoundsCovered(LatLngBounds bounds) {
  if (_cachedBounds == null || _cacheTime == null) return false;

  // Check if cache is still valid.
  final elapsed = DateTime.now().difference(_cacheTime!);
  if (elapsed > _cacheExpiry) return false;

  // Calculate cache coverage percentage
  // Only fetch new data if more than 40% of the view is outside cache.
  final viewLatRange = bounds.north - bounds.south;
  final viewLngRange = bounds.east - bounds.west;

  // Calculate overlap.
  final overlapSouth = bounds.south.clamp(
    _cachedBounds!.south,
    _cachedBounds!.north,
  );
  final overlapNorth = bounds.north.clamp(
    _cachedBounds!.south,
    _cachedBounds!.north,
  );
  final overlapWest = bounds.west.clamp(
    _cachedBounds!.west,
    _cachedBounds!.east,
  );
  final overlapEast = bounds.east.clamp(
    _cachedBounds!.west,
    _cachedBounds!.east,
  );

  final overlapLatRange = (overlapNorth - overlapSouth).abs();
  final overlapLngRange = (overlapEast - overlapWest).abs();

  final latCoverage = viewLatRange > 0 ? overlapLatRange / viewLatRange : 0.0;
  final lngCoverage = viewLngRange > 0 ? overlapLngRange / viewLngRange : 0.0;
  final coverage = (latCoverage + lngCoverage) / 2;

  // Consider covered if at least 60% of view is in cache.
  return coverage >= 0.6;
}