Update map_camera.dart

This commit is contained in:
ASHUTOSH AGARWAL
2024-04-18 11:16:41 +05:30
parent d153d259a9
commit 7cfc529e9c

View File

@@ -1,7 +1,7 @@
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'dart:ui' as ui; import 'dart:ui' as ui;
import 'package:latlong2/latlong.dart' as lat; import 'package:latlong2/latlong.dart' as lat;
import '../../map_camera_flutter.dart'; import 'package:map_camera_flutter/map_camera_flutter.dart';
///import 'package:your_app/map_camera_flutter.dart'; // Import the file where the MapCameraLocation widget is defined ///import 'package:your_app/map_camera_flutter.dart'; // Import the file where the MapCameraLocation widget is defined
@@ -103,19 +103,7 @@ class _MapCameraLocationState extends State<MapCameraLocation> {
/// Represents geocoded location information. /// Represents geocoded location information.
String? latitudeServer; LocationData? locationData;
/// Latitude value of the current location as a string.
String? longitudeServer;
/// Longitude value of the current location as a string.
String? locationName;
/// Name of the current location as a string.
String? subLocation;
/// SubLocation of the current location as a string. /// SubLocation of the current location as a string.
@@ -123,18 +111,18 @@ class _MapCameraLocationState extends State<MapCameraLocation> {
ImageAndLocationData getImageAndLocationData() { ImageAndLocationData getImageAndLocationData() {
return ImageAndLocationData( return ImageAndLocationData(
imagePath: cameraImagePath?.path, imagePath: cameraImagePath?.path,
latitude: latitudeServer, locationData: locationData,
longitude: longitudeServer,
locationName: locationName,
subLocation: subLocation,
); );
} }
Timer? _positionTimer;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
Timer.periodic(const Duration(seconds: 1), (timer) async { _positionTimer = Timer.periodic(const Duration(seconds: 1), (timer) async {
if (mounted) {
await updatePosition(context); await updatePosition(context);
}
}); });
// Initialize the camera controller // Initialize the camera controller
@@ -154,9 +142,24 @@ class _MapCameraLocationState extends State<MapCameraLocation> {
void dispose() { void dispose() {
_controller.dispose(); _controller.dispose();
_followCurrentLocationStreamController.close(); _followCurrentLocationStreamController.close();
_positionTimer?.cancel();
super.dispose(); super.dispose();
} }
@override
void setState(fn) {
if (mounted) {
super.setState(fn);
} else {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
if (mounted) {
super.setState(fn);
}
});
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@@ -165,7 +168,8 @@ class _MapCameraLocationState extends State<MapCameraLocation> {
future: _initializeControllerFuture, future: _initializeControllerFuture,
builder: (context, snapshot) { builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) { if (snapshot.connectionState == ConnectionState.done) {
return RepaintBoundary( return Center(
child: RepaintBoundary(
key: globalKey, key: globalKey,
child: Stack( child: Stack(
children: [ children: [
@@ -185,18 +189,19 @@ class _MapCameraLocationState extends State<MapCameraLocation> {
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Padding( Padding(
padding: padding: const EdgeInsets.symmetric(
const EdgeInsets.symmetric(horizontal: 8.0), horizontal: 8.0),
child: Card( child: Card(
elevation: 3, elevation: 3,
shape: RoundedRectangleBorder( shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0)), borderRadius:
BorderRadius.circular(8.0)),
child: SizedBox( child: SizedBox(
// height: 130, // height: 130,
width: 120, width: 120,
child: Padding( child: Padding(
padding: const EdgeInsets.all(5.0), padding: const EdgeInsets.all(5.0),
child: latitudeServer == null child: locationData == null
? const Center( ? const Center(
child: child:
CircularProgressIndicator()) CircularProgressIndicator())
@@ -250,7 +255,8 @@ class _MapCameraLocationState extends State<MapCameraLocation> {
CrossAxisAlignment.start, CrossAxisAlignment.start,
children: [ children: [
Text( Text(
locationName ?? "Loading...", locationData?.locationName ??
"Loading...",
maxLines: 1, maxLines: 1,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
softWrap: false, softWrap: false,
@@ -263,7 +269,8 @@ class _MapCameraLocationState extends State<MapCameraLocation> {
height: 5, height: 5,
), ),
Text( Text(
subLocation ?? "Loading ..", locationData?.subLocation ??
"Loading ..",
maxLines: 2, maxLines: 2,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
softWrap: false, softWrap: false,
@@ -276,7 +283,7 @@ class _MapCameraLocationState extends State<MapCameraLocation> {
height: 5, height: 5,
), ),
Text( Text(
"Lat ${latitudeServer ?? "Loading.."}", "Lat ${locationData?.latitude ?? "Loading.."}",
maxLines: 2, maxLines: 2,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
softWrap: false, softWrap: false,
@@ -289,7 +296,7 @@ class _MapCameraLocationState extends State<MapCameraLocation> {
height: 5, height: 5,
), ),
Text( Text(
"Long ${longitudeServer ?? "Loading.."}", "Long ${locationData?.longitude ?? "Loading.."}",
maxLines: 2, maxLines: 2,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
softWrap: false, softWrap: false,
@@ -326,6 +333,7 @@ class _MapCameraLocationState extends State<MapCameraLocation> {
), ),
], ],
), ),
),
); );
} else { } else {
return const Center(child: CircularProgressIndicator()); return const Center(child: CircularProgressIndicator());
@@ -386,10 +394,7 @@ class _MapCameraLocationState extends State<MapCameraLocation> {
if (widget.onImageCaptured != null) { if (widget.onImageCaptured != null) {
ImageAndLocationData data = ImageAndLocationData( ImageAndLocationData data = ImageAndLocationData(
imagePath: imgFile.path, imagePath: imgFile.path,
latitude: latitudeServer, locationData: locationData,
longitude: longitudeServer,
locationName: locationName,
subLocation: subLocation,
); );
widget.onImageCaptured!(data); widget.onImageCaptured!(data);
} }
@@ -411,39 +416,43 @@ class _MapCameraLocationState extends State<MapCameraLocation> {
final placeMarks = final placeMarks =
await placemarkFromCoordinates(position.latitude, position.longitude); await placemarkFromCoordinates(position.latitude, position.longitude);
LocationData locationData;
if (placeMarks.isNotEmpty) { if (placeMarks.isNotEmpty) {
final placeMark = placeMarks.first; final placeMark = placeMarks.first;
locationData = LocationData(
latitude: position.latitude.toString(),
longitude: position.longitude.toString(),
locationName:
"${placeMark.locality ?? ""}, ${placeMark.administrativeArea ?? ""}, ${placeMark.country ?? ""}",
subLocation:
"${placeMark.street ?? ""}, ${placeMark.thoroughfare ?? ""} ${placeMark.administrativeArea ?? ""}");
} else {
locationData = LocationData(
longitude: null,
latitude: null,
locationName: 'No Location Data',
subLocation: "");
}
if (locationData != this.locationData) {
// Update the state variables with the retrieved location data // Update the state variables with the retrieved location data
setState(() { setState(() {
latitudeServer = position.latitude.toString(); this.locationData = locationData;
longitudeServer = position.longitude.toString();
locationName =
"${placeMark.locality ?? ""}, ${placeMark.administrativeArea ?? ""}, ${placeMark.country ?? ""}";
subLocation =
"${placeMark.street ?? ""}, ${placeMark.thoroughfare ?? ""} ${placeMark.administrativeArea ?? ""}";
}); });
}
if (kDebugMode) { if (kDebugMode) {
print( print(
"Latitude: $latitudeServer, Longitude: $longitudeServer, Location: $locationName"); "Latitude: ${locationData.latitude}, Longitude: ${locationData.longitude}, Location: ${locationData.locationName}");
}
} else {
// Handle case when no placeMark is available
setState(() {
latitudeServer = null;
longitudeServer = null;
locationName = 'No Location Data';
subLocation = '';
});
} }
} catch (e) { } catch (e) {
// Handle any errors that occurred during location retrieval // Handle any errors that occurred during location retrieval
setState(() { setState(() {
latitudeServer = null; locationData = LocationData(
longitudeServer = null; longitude: null,
locationName = 'Error Retrieving Location'; latitude: null,
subLocation = ''; locationName: 'Error Retrieving Location',
subLocation: "");
}); });
} }
} }