getLocationDataFeed method

Stream<MyLocation> getLocationDataFeed (
  1. {String locationServiceProvider: LocationServiceProvider.LocationManagerBasedLocation,
  2. String locationProvider: LocationProvider.GPS}
)

We start location data feed request here. This method takes two optional positional parameters locationServiceProvider and locationProvider locationServiceProvider -> which is nothing but location service provider selector i.e. whether to get location data by using GoogleMobileServices based Fused Location Provider or platform based LocationManager locationProvider -> which identifies what kind of location service i.e. GPS/ Network, to use when we want to get platform's LocationManager based Location data. in case of GMSBasedLocationService, no need to send this parameter *** Remember one important thing, GMSBasedLocationService requires Access_Fine_Location permission.***

Implementation

Stream<MyLocation> getLocationDataFeed(
    {String locationServiceProvider:
        LocationServiceProvider.LocationManagerBasedLocation,
    String locationProvider: LocationProvider.GPS}) {
  MyLocation extractLocationData(dynamic event) => MyLocation(
      event['longitude'],
      event['latitude'],
      DateTime.fromMillisecondsSinceEpoch(event['time'], isUtc: true),
      event['altitude'],
      event['bearing'],
      event['speed'],
      event['accuracy'],
      event['verticalAccuracy'],
      event['bearingAccuracy'],
      event['speedAccuracy'],
      event['provider'],
      event['satelliteCount']);

  onData(dynamic event) {
    if (!streamController.isClosed && !streamController.isPaused)
      streamController.add(extractLocationData(event));
  }

  /// closes stream of data
  stop() => streamController.close();

  /// cancels location update and closes stream of location data
  cancel() async {
    try {
      await _methodChannel.invokeMethod('stopLocationUpdate').then((result) {
        if (result == 1) {
          _eventChannel = null;
          _areWeGettingLocationUpdate = false;
        }
      });
    } on PlatformException {} finally {
      stop();
    }
  }

  /// initializes location data feed subscription
  init() {
    try {
      if (!areWeGettingLocationUpdate()) {
        _eventChannel =
            const EventChannel('io.github.itzmeanjan.locate.eventChannel');
        _methodChannel.invokeMethod('startLocationUpdate', <String, String>{
          'locationServiceProvider': locationServiceProvider,
          'locationProvider': locationProvider
        }).then((dynamic result) {
          if (result == 1) {
            _eventChannel.receiveBroadcastStream().listen(
                  onData,
                  onError: (e) => streamController
                      .addError("Error: Can't Get Location Data"),
                ); // registers broadcastListener
            _areWeGettingLocationUpdate = true;
          }
        });
      }
    } on PlatformException {
      stop();
    }
  }

  if (!areWeGettingLocationUpdate()) {
    streamController = StreamController<MyLocation>(
      onCancel: cancel,
      onListen: init,
    );
  }

  return streamController.stream;
}