From 56c07852f9b18b2165e25917f5bab7041a106b5e Mon Sep 17 00:00:00 2001 From: Mohamed Nouffer Date: Thu, 7 Dec 2023 10:20:44 +0530 Subject: [PATCH] changed GPS listener --- .../destination/destination_controller.dart | 84 ++++++++----------- .../destination_map/destination_map_page.dart | 2 +- lib/pages/gps/gps_page.dart | 2 +- lib/pages/index/index_binding.dart | 2 + lib/pages/loading/loading_page.dart | 15 ++-- lib/utils/location_controller.dart | 32 ++++--- lib/widgets/map_widget.dart | 2 +- 7 files changed, 70 insertions(+), 69 deletions(-) diff --git a/lib/pages/destination/destination_controller.dart b/lib/pages/destination/destination_controller.dart index 3c65609..75c489b 100644 --- a/lib/pages/destination/destination_controller.dart +++ b/lib/pages/destination/destination_controller.dart @@ -21,6 +21,7 @@ import 'package:rogapp/services/maxtrix_service.dart'; import 'package:rogapp/services/perfecture_service.dart'; import 'package:rogapp/utils/database_gps.dart'; import 'package:rogapp/utils/database_helper.dart'; +import 'package:rogapp/utils/location_controller.dart'; import 'package:rogapp/widgets/bottom_sheet_new.dart'; import 'dart:async'; @@ -72,6 +73,7 @@ class DestinationController extends GetxController { final photos = [].obs; final IndexController indexController = Get.find(); + final LocationController locationController = Get.put(LocationController()); int _start = 0; int chekcs = 0; @@ -523,54 +525,10 @@ class DestinationController extends GetxController { Future checkForCheckin() async { //print("--- Start of checkForCheckin function ---"); + await Future.delayed(const Duration(milliseconds: 3000)); game_started = true; try { - //print("--- 000 ---- $skip_gps----"); - await Future.delayed(const Duration(milliseconds: 3000)); - Position position = await Geolocator.getCurrentPosition( - desiredAccuracy: LocationAccuracy.bestForNavigation); - - currentLat = position.latitude; - currentLon = position.longitude; - final la = position.latitude; - final ln = position.longitude; - - LogManager().addLog( - "GPS : $currentLat, $currentLon - ${DateTime.now().hour}:${DateTime.now().minute}:${DateTime.now().second}:${DateTime.now().microsecond}"); - - //print("--- gps is ${la}, ${ln}"); - - //add gps to database - //check distance to previous point - var distance = const Distance(); - double distanceToDest = distance.as( - LengthUnit.Meter, - LatLng(position.latitude, position.longitude), - LatLng(currentLat, currentLon)); - Duration difference = - lastGPSCollectedTime.difference(DateTime.now()).abs(); - if (difference.inSeconds >= 10 || distanceToDest >= 10) { - print( - "^^^^^^^^ GPS data collected ${DateFormat('kk:mm:ss \n EEE d MMM').format(DateTime.now())}, ^^^ ${position.latitude}, ${position.longitude}"); - - await addGPStoDB(la, ln); - lastGPSCollectedTime = DateTime.now(); - } - - // GpsDatabaseHelper db = GpsDatabaseHelper.instance; - // final team_name = indexController.currentUser[0]["user"]['team_name']; - // final event_code = indexController.currentUser[0]["user"]["event_code"]; - // print("--- curr gps is ${la}, ${ln}"); - // GpsData gps_data = GpsData( - // id: 0, - // team_name: team_name, - // event_code: event_code, - // lat: la, - // lon: ln, - // created_at: DateTime.now().microsecondsSinceEpoch); - // await db.insertGps(gps_data); - indexController.locations[0].features.forEach((fs) async { GeoJSONMultiPoint mp = fs!.geometry as GeoJSONMultiPoint; LatLng pt = LatLng(mp.coordinates[0][1], mp.coordinates[0][0]); @@ -578,8 +536,8 @@ class DestinationController extends GetxController { double latFs = pt.latitude; double lonFs = pt.longitude; var distanceFs = const Distance(); - double distFs = distanceFs.as( - LengthUnit.Meter, LatLng(latFs, lonFs), LatLng(la, ln)); + double distFs = distanceFs.as(LengthUnit.Meter, LatLng(latFs, lonFs), + LatLng(currentLat, currentLon)); Destination des = festuretoDestination(fs); if (distFs <= des.checkin_radious! && skipGps == false) { @@ -729,6 +687,7 @@ class DestinationController extends GetxController { Future startGame() async { //print("------ starting game ------"); + if (game_started == false) { await checkForCheckin(); } @@ -736,10 +695,41 @@ class DestinationController extends GetxController { @override void onInit() async { + ever(locationController.currentPosition, handleLocationUpdate); startGame(); super.onInit(); } + @override + void onClose() { + locationController.stopPositionStream(); + } + + void handleLocationUpdate(Position? position) async { + if (position != null) { + var distance = const Distance(); + double distanceToDest = distance.as( + LengthUnit.Meter, + LatLng(position.latitude, position.longitude), + LatLng(currentLat, currentLon)); + Duration difference = + lastGPSCollectedTime.difference(DateTime.now()).abs(); + if (difference.inSeconds >= 10 || distanceToDest >= 10) { + // print( + // "^^^^^^^^ GPS data collected ${DateFormat('kk:mm:ss \n EEE d MMM').format(DateTime.now())}, ^^^ ${position.latitude}, ${position.longitude}"); + + LogManager().addLog( + "GPS : $currentLat, $currentLon - ${DateTime.now().hour}:${DateTime.now().minute}:${DateTime.now().second}:${DateTime.now().microsecond}"); + + await addGPStoDB(position.latitude, position.longitude); + lastGPSCollectedTime = DateTime.now(); + } + + currentLat = position.latitude; + currentLon = position.longitude; + } + } + double distanceToStart() { //print("=== gfs len == ${indexController.locations[0].collection.length}"); double distanceToDest = double.infinity; diff --git a/lib/pages/destination_map/destination_map_page.dart b/lib/pages/destination_map/destination_map_page.dart index cd3cf05..75c9b05 100644 --- a/lib/pages/destination_map/destination_map_page.dart +++ b/lib/pages/destination_map/destination_map_page.dart @@ -42,7 +42,7 @@ class DestinationMapPage extends StatelessWidget { //print("^^^^ $d ^^^^"); Marker m = Marker( point: LatLng(d.lat!, d.lon!), - alignment: Alignment.topCenter, + alignment: Alignment.center, child: InkWell( onTap: () { //print("-- Destination is --- ${d.name} ------"); diff --git a/lib/pages/gps/gps_page.dart b/lib/pages/gps/gps_page.dart index 117ca61..c1bf7a6 100644 --- a/lib/pages/gps/gps_page.dart +++ b/lib/pages/gps/gps_page.dart @@ -126,7 +126,7 @@ class _GpsPageState extends State { MarkerLayer( markers: gpsData.map((i) { return Marker( - alignment: Alignment.topCenter, + alignment: Alignment.center, height: 32.0, width: 120.0, point: LatLng(i.lat, i.lon), diff --git a/lib/pages/index/index_binding.dart b/lib/pages/index/index_binding.dart index 416072e..2d76961 100644 --- a/lib/pages/index/index_binding.dart +++ b/lib/pages/index/index_binding.dart @@ -1,11 +1,13 @@ import 'package:get/get.dart'; import 'package:rogapp/pages/destination/destination_controller.dart'; import 'package:rogapp/pages/index/index_controller.dart'; +import 'package:rogapp/utils/location_controller.dart'; class IndexBinding extends Bindings { @override void dependencies() { Get.put(IndexController()); + Get.put(LocationController()); Get.put(DestinationController()); } } diff --git a/lib/pages/loading/loading_page.dart b/lib/pages/loading/loading_page.dart index 5444425..82951e1 100644 --- a/lib/pages/loading/loading_page.dart +++ b/lib/pages/loading/loading_page.dart @@ -1,16 +1,15 @@ import 'package:flutter/material.dart'; class LoadingPage extends StatelessWidget { - const LoadingPage({ Key? key }) : super(key: key); + const LoadingPage({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Container( - alignment: Alignment.topCenter, - margin: const EdgeInsets.only(top: 20), - child: const CircularProgressIndicator( - value: 0.8, - ) - ); + alignment: Alignment.center, + margin: const EdgeInsets.only(top: 20), + child: const CircularProgressIndicator( + value: 0.8, + )); } -} \ No newline at end of file +} diff --git a/lib/utils/location_controller.dart b/lib/utils/location_controller.dart index 1ba6104..4b691e9 100644 --- a/lib/utils/location_controller.dart +++ b/lib/utils/location_controller.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:geolocator/geolocator.dart'; +import 'package:rogapp/widgets/debug_widget.dart'; class LocationController extends GetxController { // Reactive variable to hold the current position @@ -97,20 +98,29 @@ class LocationController extends GetxController { } // Set up the location options - const locationOptions = LocationSettings( - accuracy: LocationAccuracy.bestForNavigation, - distanceFilter: 0, - timeLimit: Duration(microseconds: 2500), - ); + const locationOptions = + LocationSettings(accuracy: LocationAccuracy.high, distanceFilter: 10); // Start listening to the position stream - positionStream = - Geolocator.getPositionStream(locationSettings: locationOptions).listen( - (Position position) { + // positionStream = + // Geolocator.getPositionStream(locationSettings: locationOptions).listen( + // (Position position) { + // currentPosition.value = position; + // }, onError: (e) { + // // Handle errors or exceptions in the stream + // // You might want to log this error or use a state to show error messages + // }); + + StreamSubscription positionStream = + Geolocator.getPositionStream(locationSettings: locationOptions) + .listen((Position? position) { + // print(position == null + // ? 'Unknown' + // : '${position.latitude.toString()}, ${position.longitude.toString()}'); currentPosition.value = position; - }, onError: (e) { - // Handle errors or exceptions in the stream - // You might want to log this error or use a state to show error messages + + // LogManager().addLog( + // "GPS : ${position!.latitude.toString()}, ${position.longitude.toString()} - ${DateTime.now().hour}:${DateTime.now().minute}:${DateTime.now().second}:${DateTime.now().microsecond}"); }); // Resume stream if it was paused previously diff --git a/lib/widgets/map_widget.dart b/lib/widgets/map_widget.dart index 501ae63..cc2a2d9 100644 --- a/lib/widgets/map_widget.dart +++ b/lib/widgets/map_widget.dart @@ -215,7 +215,7 @@ class MapWidget extends StatelessWidget { //print( // "lat is ${p.geoSerie!.geoPoints[0].latitude} and lon is ${p.geoSerie!.geoPoints[0].longitude}"); return Marker( - alignment: Alignment.topCenter, + alignment: Alignment.center, height: 32.0, width: 120.0, point: LatLng(