startGeofenceService static method

dynamic startGeofenceService({
  1. required String pointedLatitude,
  2. required String pointedLongitude,
  3. required String radiusMeter,
  4. required int eventPeriodInSeconds,
})

startGeofenceService To start the geofence service this method takes this required following parameters pointedLatitude is the latitude of geofencing area center which is String datatype pointedLongitude is the longitude of geofencing area center which is String datatype radiusMeter is the radius of geofencing area in meters radiusMeter takes value is in String datatype and eventPeriodInSeconds will determine whether the stream listener period in seconds eventPeriodInSeconds takes value in int The purpose of this method is to initialize and start the geofencing service. At first it will check location permission and if enabled then it will start listening the current location changes then calculate the distance of changes point to the allocated geofencing area points

Implementation

static startGeofenceService(
    {required String pointedLatitude,
    required String pointedLongitude,
    required String radiusMeter,
    required int eventPeriodInSeconds}) async {
  double latitude = _parser(pointedLatitude);
  double longitude = _parser(pointedLongitude);
  double radiusInMeter = _parser(radiusMeter);
  LocationPermission permission = await Geolocator.checkPermission();
  if (permission == LocationPermission.denied) {
    permission = await Geolocator.requestPermission();
    if (permission == LocationPermission.denied) return;
  }
  if (permission == LocationPermission.deniedForever) return;
  if (_positionStream == null) {
    _geostream = _controller.stream;
    _positionStream = Geolocator.getPositionStream(
            locationSettings:
                LocationSettings(accuracy: LocationAccuracy.best))
        .listen((Position? position) {
      if (position != null) {
        double distanceInMeters = Geolocator.distanceBetween(
            latitude, longitude, position.latitude, position.longitude);
        _checkGeofence(distanceInMeters, radiusInMeter);
        _positionStream!
            .pause(Future.delayed(Duration(seconds: eventPeriodInSeconds)));
      }
    });
    _controller.add(GeofenceEvent.init);
  }
}