From dcba6a34f0406b7498c2a0b70b356068b3294b97 Mon Sep 17 00:00:00 2001
From: Maximilian Betz <Maximilian.Betz@awi.de>
Date: Fri, 11 Feb 2022 13:36:34 +0100
Subject: [PATCH] get gnss postion works

---
 README.md                                |  7 +++
 android/app/src/main/AndroidManifest.xml |  2 +
 lib/addevent.dart                        | 72 +++++++++++++++++++-----
 pubspec.lock                             | 60 +++++++++++++++++---
 pubspec.yaml                             |  2 +-
 5 files changed, 120 insertions(+), 23 deletions(-)

diff --git a/README.md b/README.md
index 70dd01b..4486d17 100644
--- a/README.md
+++ b/README.md
@@ -31,3 +31,10 @@ A cross plattform project for android and ios to log events offline everywhere a
 - Textfield to show current locally selected and offline available collection  
 - Button "get Collection devices from Sensor"
 - Button "store Collection devices locally"
+
+
+
+
+## GNSS
+- Checkbox on add event page which activates GNSS locationing. A field shall signal "no fix" or presision in meter. 
+The fields lat, long, elevation are updated automatically if checkbox is checked. 
\ No newline at end of file
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index 28156ba..d194cca 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -33,4 +33,6 @@
     </application>
     <!-- Required to fetch data from the internet. -->
     <uses-permission android:name="android.permission.INTERNET" />
+    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
+    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
 </manifest>
diff --git a/lib/addevent.dart b/lib/addevent.dart
index 64b9bfd..c469156 100644
--- a/lib/addevent.dart
+++ b/lib/addevent.dart
@@ -1,9 +1,35 @@
 import 'package:flutter/cupertino.dart';
 import 'package:flutter/material.dart';
 import 'datamodel.dart';
+import 'dart:async';
+import 'package:geolocator/geolocator.dart';
 
 
-class AddEvent extends StatelessWidget {
+Future<Position> fetchPosition() async {
+  Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
+  print(position.longitude); //Output: 80.24599079
+  print(position.latitude); //Output: 29.6593457
+  print(position.altitude);
+
+  String long = position.longitude.toString();
+  String lat = position.latitude.toString();
+  String alt = position.altitude.toString();
+
+  return position;
+}
+
+class AddEvent extends StatefulWidget {
+  @override
+  State<AddEvent> createState() => _AddEventPageState();
+}
+
+class _AddEventPageState extends State<AddEvent>  {
+  final List<bool> _isSelected = [true];
+
+  @override
+  void initState() {
+    super.initState();
+  }
 
   void _storeCurrentEvent() {
     final EventStoreInstance storedEvents = EventStoreInstance();
@@ -30,6 +56,7 @@ class AddEvent extends StatelessWidget {
     final EventStoreInstance storedEvents = EventStoreInstance();
     final EventCurrentInstance currentEvent = EventCurrentInstance();
 
+
     return Scaffold(
       appBar: AppBar(title: const Text("Add Event")),
       body:
@@ -51,12 +78,12 @@ class AddEvent extends StatelessWidget {
                   labelText: 'Event Type',
                 ),
                 items:
-                  eventTypes.store.map((EventType event) {
-                    return DropdownMenuItem(
-                      value: event.name,
-                      child: Text(event.name),
-                    );
-                  }).toList(),
+                eventTypes.store.map((EventType event) {
+                  return DropdownMenuItem(
+                    value: event.name,
+                    child: Text(event.name),
+                  );
+                }).toList(),
                 onChanged: (value) {
                   currentEvent.store.type = value.toString();
                 }
@@ -101,17 +128,36 @@ class AddEvent extends StatelessWidget {
               decoration: const InputDecoration(
                   labelText: 'Elevation'
               ),
-            )
+            ),
+            FutureBuilder<Position>(
+                future: fetchPosition(),
+                builder: (context, snapshot){
+                  if (snapshot.hasData)
+                  {
+                    return Text(snapshot.data!.latitude.toString());
+                  }
+                  else{
+                    return CircularProgressIndicator();
+                  }
+                }
+            ),
           ]),
       bottomNavigationBar: Row(
         mainAxisAlignment: MainAxisAlignment.end,
         children: [
-          FloatingActionButton(
-            heroTag: null,
-            onPressed: null,
-            tooltip: 'Get GNSS from device',
-            child: Icon(Icons.my_location),
+
+          ToggleButtons(
+            children: <Widget>[
+              Icon(Icons.my_location),
+            ],
+            isSelected: _isSelected,
+            onPressed: (int index) {
+              setState(() {
+                _isSelected[index] = !_isSelected[index];
+              });
+            },
           ),
+
           SizedBox(width: 50),
           FloatingActionButton(
             heroTag: null,
diff --git a/pubspec.lock b/pubspec.lock
index 4fae244..5f39eac 100644
--- a/pubspec.lock
+++ b/pubspec.lock
@@ -93,6 +93,48 @@ packages:
     description: flutter
     source: sdk
     version: "0.0.0"
+  geolocator:
+    dependency: "direct main"
+    description:
+      name: geolocator
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "8.1.1"
+  geolocator_android:
+    dependency: transitive
+    description:
+      name: geolocator_android
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "3.0.2"
+  geolocator_apple:
+    dependency: transitive
+    description:
+      name: geolocator_apple
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "2.0.1"
+  geolocator_platform_interface:
+    dependency: transitive
+    description:
+      name: geolocator_platform_interface
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "4.0.2"
+  geolocator_web:
+    dependency: transitive
+    description:
+      name: geolocator_web
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "2.1.4"
+  geolocator_windows:
+    dependency: transitive
+    description:
+      name: geolocator_windows
+      url: "https://pub.dartlang.org"
+    source: hosted
+    version: "0.1.0"
   http:
     dependency: "direct main"
     description:
@@ -190,35 +232,35 @@ packages:
       name: shared_preferences
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "2.0.12"
+    version: "2.0.13"
   shared_preferences_android:
     dependency: transitive
     description:
       name: shared_preferences_android
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "2.0.10"
+    version: "2.0.11"
   shared_preferences_ios:
     dependency: transitive
     description:
       name: shared_preferences_ios
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "2.0.9"
+    version: "2.0.10"
   shared_preferences_linux:
     dependency: transitive
     description:
       name: shared_preferences_linux
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "2.0.4"
+    version: "2.1.0"
   shared_preferences_macos:
     dependency: transitive
     description:
       name: shared_preferences_macos
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "2.0.2"
+    version: "2.0.3"
   shared_preferences_platform_interface:
     dependency: transitive
     description:
@@ -239,7 +281,7 @@ packages:
       name: shared_preferences_windows
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "2.0.4"
+    version: "2.1.0"
   sky_engine:
     dependency: transitive
     description: flutter
@@ -307,14 +349,14 @@ packages:
       name: win32
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "2.3.6"
+    version: "2.3.11"
   xdg_directories:
     dependency: transitive
     description:
       name: xdg_directories
       url: "https://pub.dartlang.org"
     source: hosted
-    version: "0.2.0"
+    version: "0.2.0+1"
 sdks:
   dart: ">=2.15.1 <3.0.0"
-  flutter: ">=2.5.0"
+  flutter: ">=2.8.0"
diff --git a/pubspec.yaml b/pubspec.yaml
index 60438c9..87d46a2 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -32,7 +32,7 @@ dependencies:
 
   shared_preferences: ^2.0.12
   http: ^0.13.4
-
+  geolocator: ^8.1.1
 
   # The following adds the Cupertino Icons font to your application.
   # Use with the CupertinoIcons class for iOS style icons.
-- 
GitLab