diff --git a/lib/page/capture_page.dart b/lib/page/capture_page.dart index c08e9ce..baa079c 100644 --- a/lib/page/capture_page.dart +++ b/lib/page/capture_page.dart @@ -15,6 +15,13 @@ class _CapturePageState extends State { bool _isRearCameraSelected = true; final List _imgListCaptured = []; + Stream? _positionStream; + Position? _currentPosition; + final LocationSettings locationSettings = LocationSettings( + accuracy: LocationAccuracy.high, + distanceFilter: 2, //The minimum distance (in meters) to trigger an update + ); + @override void dispose() { _cameraController.dispose(); @@ -25,11 +32,31 @@ class _CapturePageState extends State { void initState() { super.initState(); + startLocationUpdates(); + if (widget.cameras?.isNotEmpty ?? false) { initCamera(widget.cameras![0]); } } + void startLocationUpdates() async { + if (await PermissionHelper.isPermissionGranted()) { + _positionStream = + Geolocator.getPositionStream(locationSettings: locationSettings); + + if (_positionStream != null) { + _positionStream!.listen((Position position) { + setState(() { + _currentPosition = position; + }); + }); + } + } else { + await PermissionHelper.askMissingPermission(); + startLocationUpdates(); + } + } + void goToCollectionCreationPage() { context.push(Routes.newSequenceSend, extra: _imgListCaptured); } @@ -45,22 +72,16 @@ class _CapturePageState extends State { return null; } try { - if (await PermissionHelper.isPermissionGranted()) { - await Future.wait( - [ - getPictureFromCamera(), - Geolocator.getCurrentPosition(), - ], - ).then((value) async { - final XFile rawImage = value[0] as XFile; - final Position currentLocation = value[1] as Position; - await addExifTags(rawImage, currentLocation); - addImageToList(rawImage); - }); - } else { - await PermissionHelper.askMissingPermission(); - takePicture(); - } + await Future.wait( + [ + getPictureFromCamera(), + ], + ).then((value) async { + final XFile rawImage = value[0] as XFile; + final Position currentLocation = _currentPosition!; + await addExifTags(rawImage, currentLocation); + addImageToList(rawImage); + }); } on CameraException catch (e) { debugPrint('Error occurred while taking picture: $e'); return null; @@ -82,6 +103,7 @@ class _CapturePageState extends State { } Future addExifTags(XFile rawImage, Position currentLocation) async { + print(currentLocation.latitude.toString() + " " + currentLocation.longitude.toString()); final exif = FlutterExif.fromPath(rawImage.path); await exif.setLatLong(currentLocation.latitude, currentLocation.longitude); await exif.setAltitude(currentLocation.altitude); @@ -110,7 +132,7 @@ class _CapturePageState extends State { if (widget.cameras?.isEmpty ?? true) { return Scaffold( appBar: AppBar(), - body: Center( + body: Center( child: Text(AppLocalizations.of(context)!.noCameraFoundError), ), ); @@ -154,7 +176,10 @@ class _CapturePageState extends State { child: IconButton( padding: EdgeInsets.zero, iconSize: 30, - icon: Icon(_isRearCameraSelected ? CupertinoIcons.switch_camera : CupertinoIcons.switch_camera_solid, + icon: Icon( + _isRearCameraSelected + ? CupertinoIcons.switch_camera + : CupertinoIcons.switch_camera_solid, color: Colors.white), onPressed: () { setState(() => _isRearCameraSelected = !_isRearCameraSelected); @@ -171,7 +196,8 @@ class _CapturePageState extends State { iconSize: 30, icon: const Icon(Icons.send_outlined, color: Colors.white), onPressed: goToCollectionCreationPage, - tooltip: AppLocalizations.of(context)!.createSequenceWithPicture_tooltip)); + tooltip: AppLocalizations.of(context)! + .createSequenceWithPicture_tooltip)); } Widget imageCart(IconButton cartIcon) {