determinePosition method

Future<GeoPoint> determinePosition()

Determines the current position of the device. Returns a GeoPoint representing the current location. If the location services are disabled or permissions are denied, an error is returned. Uses the geolocator package for location services. param None return A GeoPoint representing the current location. Throws an error if location services are disabled or permissions are denied. Caches the position for future use. return A GeoPoint representing the current location.

Implementation

Future<GeoPoint> determinePosition() async {
  if (_myPosCache != null) {
    return _myPosCache!;
  }
  bool serviceEnabled;
  LocationPermission permission;
  serviceEnabled = await Geolocator.isLocationServiceEnabled();
  if (!serviceEnabled) {
    return Future.error('Location services are disabled.');
  }

  permission = await Geolocator.checkPermission();
  if (permission == LocationPermission.denied) {
    permission = await Geolocator.requestPermission();
    if (permission == LocationPermission.denied) {
      return Future.error('Location permissions are denied');
    }
  }

  if (permission == LocationPermission.deniedForever) {
    return Future.error(
      'Location permissions are permanently denied, we cannot request permissions.',
    );
  }

  var pos = await Geolocator.getCurrentPosition();
  _myPosCache = GeoPoint(pos.latitude, pos.longitude);
  return _myPosCache!;
}