private static int FindOptimalTileZoomLevel(int width, Models.GeographicalBounds geoBBox) { var mapSize = WebMercator.MapSize(width, geoBBox.MinLongitude, geoBBox.MaxLongitude); var minZoom = 0; var minDistance = Double.MaxValue; for (var zoom = 0; zoom < 24; zoom++) // TODO: range? { var mapSizeAtZoom = WebMercator.MapSize(zoom, WebMercator.DefaultTileSize); // TODO: ? use tile size parameter instead of const var distance = Math.Abs(mapSize - mapSizeAtZoom); if (distance < minDistance) { minDistance = distance; minZoom = zoom; } } return(minZoom); }