changed GPS listener

This commit is contained in:
2023-12-07 10:20:44 +05:30
parent fa0587178f
commit 56c07852f9
7 changed files with 70 additions and 69 deletions

View File

@ -21,6 +21,7 @@ import 'package:rogapp/services/maxtrix_service.dart';
import 'package:rogapp/services/perfecture_service.dart'; import 'package:rogapp/services/perfecture_service.dart';
import 'package:rogapp/utils/database_gps.dart'; import 'package:rogapp/utils/database_gps.dart';
import 'package:rogapp/utils/database_helper.dart'; import 'package:rogapp/utils/database_helper.dart';
import 'package:rogapp/utils/location_controller.dart';
import 'package:rogapp/widgets/bottom_sheet_new.dart'; import 'package:rogapp/widgets/bottom_sheet_new.dart';
import 'dart:async'; import 'dart:async';
@ -72,6 +73,7 @@ class DestinationController extends GetxController {
final photos = <File>[].obs; final photos = <File>[].obs;
final IndexController indexController = Get.find<IndexController>(); final IndexController indexController = Get.find<IndexController>();
final LocationController locationController = Get.put(LocationController());
int _start = 0; int _start = 0;
int chekcs = 0; int chekcs = 0;
@ -523,54 +525,10 @@ class DestinationController extends GetxController {
Future<void> checkForCheckin() async { Future<void> checkForCheckin() async {
//print("--- Start of checkForCheckin function ---"); //print("--- Start of checkForCheckin function ---");
await Future.delayed(const Duration(milliseconds: 3000));
game_started = true; game_started = true;
try { 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 { indexController.locations[0].features.forEach((fs) async {
GeoJSONMultiPoint mp = fs!.geometry as GeoJSONMultiPoint; GeoJSONMultiPoint mp = fs!.geometry as GeoJSONMultiPoint;
LatLng pt = LatLng(mp.coordinates[0][1], mp.coordinates[0][0]); LatLng pt = LatLng(mp.coordinates[0][1], mp.coordinates[0][0]);
@ -578,8 +536,8 @@ class DestinationController extends GetxController {
double latFs = pt.latitude; double latFs = pt.latitude;
double lonFs = pt.longitude; double lonFs = pt.longitude;
var distanceFs = const Distance(); var distanceFs = const Distance();
double distFs = distanceFs.as( double distFs = distanceFs.as(LengthUnit.Meter, LatLng(latFs, lonFs),
LengthUnit.Meter, LatLng(latFs, lonFs), LatLng(la, ln)); LatLng(currentLat, currentLon));
Destination des = festuretoDestination(fs); Destination des = festuretoDestination(fs);
if (distFs <= des.checkin_radious! && skipGps == false) { if (distFs <= des.checkin_radious! && skipGps == false) {
@ -729,6 +687,7 @@ class DestinationController extends GetxController {
Future<void> startGame() async { Future<void> startGame() async {
//print("------ starting game ------"); //print("------ starting game ------");
if (game_started == false) { if (game_started == false) {
await checkForCheckin(); await checkForCheckin();
} }
@ -736,10 +695,41 @@ class DestinationController extends GetxController {
@override @override
void onInit() async { void onInit() async {
ever(locationController.currentPosition, handleLocationUpdate);
startGame(); startGame();
super.onInit(); 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() { double distanceToStart() {
//print("=== gfs len == ${indexController.locations[0].collection.length}"); //print("=== gfs len == ${indexController.locations[0].collection.length}");
double distanceToDest = double.infinity; double distanceToDest = double.infinity;

View File

@ -42,7 +42,7 @@ class DestinationMapPage extends StatelessWidget {
//print("^^^^ $d ^^^^"); //print("^^^^ $d ^^^^");
Marker m = Marker( Marker m = Marker(
point: LatLng(d.lat!, d.lon!), point: LatLng(d.lat!, d.lon!),
alignment: Alignment.topCenter, alignment: Alignment.center,
child: InkWell( child: InkWell(
onTap: () { onTap: () {
//print("-- Destination is --- ${d.name} ------"); //print("-- Destination is --- ${d.name} ------");

View File

@ -126,7 +126,7 @@ class _GpsPageState extends State<GpsPage> {
MarkerLayer( MarkerLayer(
markers: gpsData.map((i) { markers: gpsData.map((i) {
return Marker( return Marker(
alignment: Alignment.topCenter, alignment: Alignment.center,
height: 32.0, height: 32.0,
width: 120.0, width: 120.0,
point: LatLng(i.lat, i.lon), point: LatLng(i.lat, i.lon),

View File

@ -1,11 +1,13 @@
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:rogapp/pages/destination/destination_controller.dart'; import 'package:rogapp/pages/destination/destination_controller.dart';
import 'package:rogapp/pages/index/index_controller.dart'; import 'package:rogapp/pages/index/index_controller.dart';
import 'package:rogapp/utils/location_controller.dart';
class IndexBinding extends Bindings { class IndexBinding extends Bindings {
@override @override
void dependencies() { void dependencies() {
Get.put<IndexController>(IndexController()); Get.put<IndexController>(IndexController());
Get.put<LocationController>(LocationController());
Get.put<DestinationController>(DestinationController()); Get.put<DestinationController>(DestinationController());
} }
} }

View File

@ -6,11 +6,10 @@ class LoadingPage extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Container( return Container(
alignment: Alignment.topCenter, alignment: Alignment.center,
margin: const EdgeInsets.only(top: 20), margin: const EdgeInsets.only(top: 20),
child: const CircularProgressIndicator( child: const CircularProgressIndicator(
value: 0.8, value: 0.8,
) ));
);
} }
} }

View File

@ -2,6 +2,7 @@ import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:geolocator/geolocator.dart'; import 'package:geolocator/geolocator.dart';
import 'package:rogapp/widgets/debug_widget.dart';
class LocationController extends GetxController { class LocationController extends GetxController {
// Reactive variable to hold the current position // Reactive variable to hold the current position
@ -97,20 +98,29 @@ class LocationController extends GetxController {
} }
// Set up the location options // Set up the location options
const locationOptions = LocationSettings( const locationOptions =
accuracy: LocationAccuracy.bestForNavigation, LocationSettings(accuracy: LocationAccuracy.high, distanceFilter: 10);
distanceFilter: 0,
timeLimit: Duration(microseconds: 2500),
);
// Start listening to the position stream // Start listening to the position stream
positionStream = // positionStream =
Geolocator.getPositionStream(locationSettings: locationOptions).listen( // Geolocator.getPositionStream(locationSettings: locationOptions).listen(
(Position position) { // (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<Position> positionStream =
Geolocator.getPositionStream(locationSettings: locationOptions)
.listen((Position? position) {
// print(position == null
// ? 'Unknown'
// : '${position.latitude.toString()}, ${position.longitude.toString()}');
currentPosition.value = position; currentPosition.value = position;
}, onError: (e) {
// Handle errors or exceptions in the stream // LogManager().addLog(
// You might want to log this error or use a state to show error messages // "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 // Resume stream if it was paused previously

View File

@ -215,7 +215,7 @@ class MapWidget extends StatelessWidget {
//print( //print(
// "lat is ${p.geoSerie!.geoPoints[0].latitude} and lon is ${p.geoSerie!.geoPoints[0].longitude}"); // "lat is ${p.geoSerie!.geoPoints[0].latitude} and lon is ${p.geoSerie!.geoPoints[0].longitude}");
return Marker( return Marker(
alignment: Alignment.topCenter, alignment: Alignment.center,
height: 32.0, height: 32.0,
width: 120.0, width: 120.0,
point: LatLng( point: LatLng(