precision medium - more fast geolocation

This commit is contained in:
Valentin CZERYBA 2024-11-20 23:19:35 +01:00
parent 4d3533eb8a
commit 4a313fc725
2 changed files with 77 additions and 45 deletions

View File

@ -1,3 +1,4 @@
import 'package:covas_mobile/classes/alert.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:flutter_dotenv/flutter_dotenv.dart'; // Import dotenv
@ -50,21 +51,37 @@ class _MyHomePageState extends State<ListItemMenu> {
// Fetching events from API
static Future<List<Events>> getPosts() async {
PermissionStatus status = await Permission.location.status;
final List<Events> body = [];
var url = Uri.parse("${globals.api}/events");
if (status.isGranted) {
print("Location permission granted");
// Get the current position with high accuracy
LocationSettings locationSettings = LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter:
10, // Optional: Minimum distance (in meters) to trigger location update
);
Position position = await Geolocator.getCurrentPosition(
locationSettings: locationSettings,
const LocationSettings locationSettings = LocationSettings(
accuracy: LocationAccuracy.medium,
timeLimit: Duration(seconds: 5),
);
Position? position;
try {
position = await Geolocator.getCurrentPosition(
locationSettings: locationSettings);
} on LocationServiceDisabledException {
// Handle location services disabled
print('Location services are disabled.');
position = await Geolocator.getLastKnownPosition();
if (position == null) {
print('No last known position available.');
}
} catch (e) {
// Handle other errors
print('Failed to get location: $e');
position = await Geolocator.getLastKnownPosition();
if (position == null) {
print('No last known position available.');
}
}
if (position != null) {
// Calculate the boundaries
double radiusInKm = 50;
double latDistance = radiusInKm / 111.0;
@ -82,7 +99,7 @@ class _MyHomePageState extends State<ListItemMenu> {
}
SharedPreferences prefs = await SharedPreferences.getInstance();
var accessToken = prefs.getString("access_token") ?? "";
final List<Events> body = [];
if (accessToken.isNotEmpty) {
final response = await http.get(url, headers: {
"Content-Type": "application/json",
@ -91,6 +108,7 @@ class _MyHomePageState extends State<ListItemMenu> {
final List body = json.decode(utf8.decode(response.bodyBytes));
return body.map((e) => Events.fromJson(e)).toList();
}
}
return body;
}

View File

@ -141,19 +141,33 @@ class _MapboxPagesState extends State<MapboxPages> with ShowErrorDialog {
"Location permissions are permanently denied. Enable them in settings.");
return;
}
LocationSettings locationSettings = LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter:
10, // Optional: Minimum distance (in meters) to trigger location update
);
Position position = await Geolocator.getCurrentPosition(
locationSettings: locationSettings,
);
const LocationSettings locationSettings = LocationSettings(
accuracy: LocationAccuracy.medium, timeLimit: Duration(seconds: 5));
Position? position;
try {
position = await Geolocator.getCurrentPosition(
locationSettings: locationSettings);
} on LocationServiceDisabledException {
// Handle location services disabled
print('Location services are disabled.');
position = await Geolocator.getLastKnownPosition();
if (position == null) {
print('No last known position available.');
}
} catch (e) {
// Handle other errors
print('Failed to get location: $e');
position = await Geolocator.getLastKnownPosition();
if (position == null) {
print('No last known position available.');
}
}
if (position != null) {
setState(() {
userPosition = LatLng(position.latitude, position.longitude);
userPosition = LatLng(position!.latitude, position!.longitude);
isUserPositionInitialized = true;
});
}
_initToken();
_getEventInfo();
} catch (e) {