59 lines
2.4 KiB
Dart
59 lines
2.4 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:geolocator/geolocator.dart';
|
|
import 'package:mzansi_innovation_hub/mih_services/mih_alert_services.dart';
|
|
|
|
class MIHLocationAPI {
|
|
final LocationSettings locationSettings = const LocationSettings(
|
|
accuracy: LocationAccuracy.high,
|
|
distanceFilter: 100,
|
|
);
|
|
|
|
///This function is to get the current location of the signed in user.
|
|
///First checks the permission, if permission is denied (new user), request permission from user.
|
|
///if user has blocked permission (denied or denied forver), user will get error pop up.
|
|
///if user has granted permission (while in use), function will return Position object.
|
|
Future<Position?> getGPSPosition(BuildContext context) async {
|
|
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
|
|
if (!serviceEnabled && Platform.isLinux) {
|
|
// Direct the user to their System Settings
|
|
MihAlertServices().locationPermissionAlert(context);
|
|
return null;
|
|
}
|
|
print("Before checkPermission"); // Debug
|
|
LocationPermission permission = await Geolocator.checkPermission();
|
|
print("After checkPermission: $permission"); // Debug
|
|
if (permission == LocationPermission.denied) {
|
|
permission = await Geolocator.requestPermission();
|
|
if (permission == LocationPermission.denied) {
|
|
MihAlertServices().locationPermissionAlert(context);
|
|
return null;
|
|
} else if (permission == LocationPermission.deniedForever) {
|
|
MihAlertServices().locationPermissionAlert(context);
|
|
return null;
|
|
} else {
|
|
Position location = await Geolocator.getCurrentPosition(
|
|
locationSettings: locationSettings);
|
|
return location;
|
|
}
|
|
} else if (permission == LocationPermission.deniedForever) {
|
|
MihAlertServices().locationPermissionAlert(context);
|
|
return null;
|
|
} else {
|
|
Position location = await Geolocator.getCurrentPosition(
|
|
locationSettings: locationSettings);
|
|
return location;
|
|
}
|
|
}
|
|
|
|
double getDistanceInMeaters(String startPosition, String endPosition) {
|
|
double startLatitude = double.parse(startPosition.split(", ")[0]);
|
|
double startLogitude = double.parse(startPosition.split(", ")[1]);
|
|
double endLatitude = double.parse(endPosition.split(", ")[0]);
|
|
double endLogitude = double.parse(endPosition.split(", ")[1]);
|
|
return Geolocator.distanceBetween(
|
|
startLatitude, startLogitude, endLatitude, endLogitude);
|
|
}
|
|
}
|