moveCamera method
Moves the camera to location, keeping the current zoom when zoom is
null.
Attribution applies as it does to fitBounds.
Implementation
@override
void moveCamera(latlong.LatLng location, double? zoom) async {
// First approach, simpler but less accurate
// final controller = await mapController.future;
// final visibleRegion = await controller.getVisibleRegion();
// // Calculate padding offset
// final paddingOffset = LatLng(
// (visibleRegion.northeast.latitude - visibleRegion.southwest.latitude) *
// (padding.top - padding.bottom) /
// viewportSize.height,
// (visibleRegion.northeast.longitude - visibleRegion.southwest.longitude) *
// (padding.left - padding.right) /
// viewportSize.width,
// );
// controller.animateCamera(
// CameraUpdate.newCameraPosition(
// CameraPosition(
// target: LatLng(
// location.latitude + paddingOffset.latitude,
// location.longitude + paddingOffset.longitude,
// ),
// zoom: zoom ?? 16,
// ),
// ),
// );
// Second approach, more complex but should be more accurate
// final c = await mapController.future;
// // Use current zoom if not provided
// final currentZoom = await c.getZoomLevel();
// final targetZoom = zoom ?? currentZoom;
// // Where the "center" of the padded viewport actually is (in screen px)
// final paddedCenterX =
// (viewportSize.width / 2) + (padding.left - padding.right) / 2;
// final paddedCenterY =
// (viewportSize.height / 2) + (padding.top - padding.bottom) / 2;
// // Find the lat/lng that appears at that padded center
// final targetScreen = ScreenCoordinate(
// x: paddedCenterX.round(),
// y: paddedCenterY.round(),
// );
// final latLngAtPaddedCenter = await c.getLatLng(targetScreen);
// // Compute the lat/lng shift needed so `location` ends up at the padded center
// final dx = location.latitude - latLngAtPaddedCenter.latitude;
// final dy = location.longitude - latLngAtPaddedCenter.longitude;
// await c.animateCamera(
// CameraUpdate.newCameraPosition(
// CameraPosition(
// target: LatLng(
// location.latitude +
// dx, // effectively centers location at padded center
// location.longitude + dy,
// ),
// zoom: targetZoom,
// ),
// ),
// );
// Third approach, simpler and should be accurate enough
await _withMap((map) async {
final currentZoom = await map.getZoomLevel();
beginProgrammaticMove();
await map.animateCamera(
CameraUpdate.newLatLngZoom(
location.toGoogleMapLatLng(), zoom ?? currentZoom),
);
});
}