Live Location Tracker Flutter

repository·master·Indexed 41 days ago

https://github.com/amanullahgit/live-location-tracker-flutter

A Flutter application for real-time location tracking using Google Maps, Firebase Firestore, and the location package. It enables users to synchronize GPS coordinates to a Firestore 'location' collection and visualize live movements across multiple devices via the MyMap widget.

Tokens
1.3K
Snippets
4
Records
7
Agent score
99%

What's inside Live Location Tracker Flutter

  1. Core dependencies for Live Location Tracking

    master

    The project relies on the following key packages and services to function:

    • Firebase: Used for storing and synchronizing location data in real-time.
    • Google Maps: Used for rendering the map interface.
    • location: Used to retrieve the device's current GPS coordinates.
    • permission_handler: Used to manage and request necessary location permissions from the user.
  2. Customize the iOS launch screen assets

    master

    To change the image displayed during the app's launch on iOS, you can either replace the image files directly in the file system or use Xcode.

    Method 1: File System Replacement

    Navigate to the ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory and replace the existing image files with your own assets. Ensure the filenames and formats match what the project expects.

    1. Open the iOS workspace in Xcode by running: open ios/Runner.xcworkspace.
    2. In the Xcode Project Navigator, locate and select Runner/Assets.xcassets.
    3. Drag and drop your new image assets into the asset catalog to replace the existing launch images.
    open ios/Runner.xcworkspace
  3. Initialize and use the Live Location Tracker

    master

    To use this project, ensure Firebase is initialized in your main() function. The application relies on FirebaseFirestore for data persistence and the location package for device coordinates.

    Key operations include:

    • Requesting Permissions: Use Permission.location.request() to handle location access. If denied, you may need to prompt the user to open app settings.
    • Single Location Update: Use location.getLocation() to fetch the current coordinates and write them to a Firestore collection (e.g., location/user1).
    • Live Tracking: Subscribe to location.onLocationChanged to receive a stream of LocationData. Each update should be written to Firestore using SetOptions(merge: true) to update existing documents without overwriting other fields.
    • Displaying Data: Use a StreamBuilder listening to FirebaseFirestore.instance.collection('location').snapshots() to reactively update the UI as location data changes in the database.
    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp();
      runApp(MaterialApp(home: MyApp()));
    }
    
    // Example of updating location to Firestore
    _getLocation() async {
        final loc.LocationData _locationResult = await location.getLocation();
        await FirebaseFirestore.instance.collection('location').doc('user1').set({
          'latitude': _locationResult.latitude,
          'longitude': _locationResult.longitude,
          'name': 'john'
        }, SetOptions(merge: true));
    }
  4. Use the MyMap widget to display live location

    master

    The MyMap widget is a StatefulWidget used to display a Google Map that tracks a specific user's live location. It listens to a real-time stream from the Firebase Firestore location collection.

    To use it, instantiate MyMap and provide the user_id of the user you wish to track. The widget will automatically find the document in the location collection where the document ID matches the provided user_id and use its latitude and longitude fields to position the map marker and camera.

    // Example usage of MyMap
    MyMap('TARGET_USER_ID');
  5. Firestore Data Schema for Locations

    master

    The application expects a Firestore collection named location. Each document in this collection represents a user's current position. Based on the implementation, a document should contain the following fields:

    FieldTypeDescription
    nameStringThe identifier or name of the user
    latitudeNumberThe current latitude coordinate
    longitudeNumberThe current longitude coordinate

    When updating locations, use SetOptions(merge: true) to ensure that existing fields (like name) are preserved when only coordinates are updated.

    {
      "name": "john",
      "latitude": 0.0,
      "longitude": 0.0
    }
  6. Data requirements for MyMap tracking

    master

    For MyMap to function correctly, your Firebase Firestore database must satisfy the following schema requirements:

    1. Collection Name: A collection named location must exist.
    2. Document ID: The document ID within the location collection must match the user_id passed to the MyMap constructor.
    3. Fields: The document must contain the following fields:
      • latitude: A numeric value representing the latitude.
      • longitude: A numeric value representing the longitude.

    If the document or the required fields are missing, the widget will fail to locate the user's position.