startSharingLocation method

Future<void> startSharingLocation(
  1. String groupId,
  2. String userId,
  3. GeoPoint location
)

//////////// //////////// Starts sharing a user's location with a group

Implementation

// II.D - Location Sharing
///////////////
/// Starts sharing a user's location with a group
Future<void> startSharingLocation(String groupId, String userId, GeoPoint location) async {
  try {
    // Check if location sharing is enabled for this group
    final groupDoc = await _firestore.collection(_collectionPath).doc(groupId).get();
    final groupData = groupDoc.data();

    if (groupData == null || groupData['locationSharingEnabled'] == false) {
      throw Exception('Location sharing is disabled for this group');
    }

    // Create or update the user's location in the group
    await _firestore
        .collection(_collectionPath)
        .doc(groupId)
        .collection(_locationSubcollection)
        .doc(userId)
        .set({
          'userId': userId,
          'location': location,
          'timestamp': FieldValue.serverTimestamp(),
          'speed': 0.0, // Optional fields
          'heading': 0.0, // Optional fields
          'accuracy': 0.0, // Optional fields
        }, SetOptions(merge: true));
  } catch (e) {
    print('Error starting location sharing: $e');
    rethrow;
  }
}