startGeofenceService static method

dynamic startGeofenceService({
  1. required String? pointedLatitude,
  2. required String? pointedLongitude,
  3. required String? radiusMeter,
  4. 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,
    int? eventPeriodInSeconds}) {
  //parsing the values to double if in any case they are coming in int etc
  double latitude = _parser(pointedLatitude);
  double longitude = _parser(pointedLongitude);
  double radiusInMeter = _parser(radiusMeter);
  //starting the geofence service only if the positionstream is null with us
  if (_positionStream == null) {
    _geoFencestream = _controller.stream;
    _positionStream = Geolocator.getPositionStream(
      desiredAccuracy: LocationAccuracy.high,
    ).listen((Position position) {
      double distanceInMeters = Geolocator.distanceBetween(
          latitude, longitude, position.latitude, position.longitude);
      _printOnConsole(
          latitude, longitude, position, distanceInMeters, radiusInMeter);
      _checkGeofence(distanceInMeters, radiusInMeter);
      _positionStream!
          .pause(Future.delayed(Duration(seconds: eventPeriodInSeconds!)));
    });
    _controller.add(GeofenceStatus.init);
  }
}