Live Location Tracker Flutter
repository·master·Indexed 41 days ago
https://github.com/amanullahgit/live-location-tracker-flutterA 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.
What's inside Live Location Tracker Flutter
- The Flutter Live Location Tracker is a project designed to track live locations using Google Maps and Firebase. It allows users to add their current location to a Firebase database and visualize that location on a Google Map, enabling real-time tracking across multiple devices.
Core dependencies for Live Location Tracking
masterThe 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.
Customize the iOS launch screen assets
masterTo 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.Method 2: Using Xcode (Recommended)
- Open the iOS workspace in Xcode by running:
open ios/Runner.xcworkspace. - In the Xcode Project Navigator, locate and select
Runner/Assets.xcassets. - Drag and drop your new image assets into the asset catalog to replace the existing launch images.
open ios/Runner.xcworkspace- Open the iOS workspace in Xcode by running:
Initialize and use the Live Location Tracker
masterTo use this project, ensure Firebase is initialized in your
main()function. The application relies onFirebaseFirestorefor data persistence and thelocationpackage 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.onLocationChangedto receive a stream ofLocationData. Each update should be written to Firestore usingSetOptions(merge: true)to update existing documents without overwriting other fields. - Displaying Data: Use a
StreamBuilderlistening toFirebaseFirestore.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)); }- Requesting Permissions: Use
Use the MyMap widget to display live location
masterThe
MyMapwidget is aStatefulWidgetused to display a Google Map that tracks a specific user's live location. It listens to a real-time stream from the Firebase Firestorelocationcollection.To use it, instantiate
MyMapand provide theuser_idof the user you wish to track. The widget will automatically find the document in thelocationcollection where the document ID matches the provideduser_idand use itslatitudeandlongitudefields to position the map marker and camera.// Example usage of MyMap MyMap('TARGET_USER_ID');Firestore Data Schema for Locations
masterThe 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:Field Type Description nameString The identifier or name of the user latitudeNumber The current latitude coordinate longitudeNumber The current longitude coordinate When updating locations, use
SetOptions(merge: true)to ensure that existing fields (likename) are preserved when only coordinates are updated.{ "name": "john", "latitude": 0.0, "longitude": 0.0 }Data requirements for MyMap tracking
masterFor
MyMapto function correctly, your Firebase Firestore database must satisfy the following schema requirements:- Collection Name: A collection named
locationmust exist. - Document ID: The document ID within the
locationcollection must match theuser_idpassed to theMyMapconstructor. - 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.
- Collection Name: A collection named