calculateFittingZoomlevel static method

int calculateFittingZoomlevel(
  1. BoundingBox boundary,
  2. Size size
)

Find the maximum zoomLevel where the boundary fits in given the current screensize. This is an expensive operations since we probe each zoomLevel until we find the correct one. Use it sparingly.

Implementation

static int calculateFittingZoomlevel(BoundingBox boundary, Size size) {
  for (int zoomLevel = Scalefactor.MAXZOOMLEVEL; zoomLevel >= 0; --zoomLevel) {
    PixelProjection projection = PixelProjection(zoomLevel);
    Mappoint leftUpper = projection.latLonToPixel(LatLong(boundary.maxLatitude, boundary.minLongitude));
    Mappoint rightBottom = projection.latLonToPixel(LatLong(boundary.minLatitude, boundary.maxLongitude));
    assert(leftUpper.x < rightBottom.x);
    assert(leftUpper.y < rightBottom.y);
    if ((rightBottom.x - leftUpper.x) <= size.width && (rightBottom.y - leftUpper.y) <= size.height) {
      return zoomLevel;
    }
  }
  return 0;
}