У меня есть карта, и поверх нее я поместил прозрачный контейнер, имеющий только границу. Пользователь может выбрать определенное количество квадратных миль (1, 3, 10, 20), и после выбора уровень масштабирования карты настраивается так, чтобы выбранное количество квадратных миль отображалось внутри контейнера.
Код: Выделить всё
Future calculateZoomLevel(int miles, int containerSize) async {
const double equatorLength =
40075016.686; // Earth's circumference in meters
const double tileSize = 256; // Tile size at zoom level 1
// Convert miles to meters
final double radiusMeters = miles * 1609.34;
// Calculate the diameter in meters that needs to fit within the container
final double diameterMeters = radiusMeters * 2;
// Get the screen density (DPI)
final double screenDensity = View.of(context).devicePixelRatio;
// Calculate the diameter in pixels based on container size and density
final double diameterPixels = containerSize * screenDensity;
// Calculate zoom level using the formula
final double zoomLevel =
log(equatorLength * diameterPixels / (diameterMeters * tileSize)) / ln2;
// Set the zoom level on the map controller
await controller.view.setLocation(zoom: zoomLevel.toDouble());
print('zoom level = $zoomLevel');
print('miles = $miles');
}
Подробнее здесь: https://stackoverflow.com/questions/784 ... -displayed