Ever wondered how your favorite apps determine your location to show you nearby services or friends?
Welcome to the world of geospatial queries with Geoflutterfire2!
In this blog, we'll explore everything you need to know about Geoflutterfire2, from setting it up to querying Firestore documents based on geographic location.
Ready to dive into the geospatial world?
Let’s get started!
Geoflutterfire2 is an open-source library that lets you save and retrieve Firestore documents based on their geolocation. This library leverages the power of Firestore to manage spatial data, providing a seamless way to query documents within a specified radius from a given point. By using geohashes, Geoflutterfire2 makes it efficient to handle location-based queries.
• Store and query Firestore documents based on geographic location.
• Efficient handling of spatial queries using geohashes.
• Real-time updates of query results as data changes in Firestore.
• Support for nested geospatial fields in Firestore documents.
To start using Geoflutterfire2 in your Flutter project, you need to add it as a dependency. In your pubspec.yaml file, add:
1dependencies: 2 geoflutterfire2: ^latest_version 3 cloud_firestore: ^latest_version
Then, run flutter pub get to install the package.
First, you need to import the necessary packages and initialize Firestore and Geoflutterfire:
1import 'package:geoflutterfire2/geoflutterfire2.dart'; 2import 'package:cloud_firestore/cloud_firestore.dart'; 3 4final geo = GeoFlutterFire(); 5final _firestore = FirebaseFirestore.instance;
To store a location in Firestore, you need to create a GeoFirePoint:
1GeoFirePoint myLocation = geo.point(latitude: 12.960632, longitude: 77.641603); 2 3_firestore.collection('locations').add({ 4 'name': 'random name', 5 'position': myLocation.data 6});
This will store the location data, including a geohash string and a Firestore GeoPoint.
Querying Firestore documents based on geographic location to return documents within a specified radius is where Geoflutterfire2 truly shines. You can query documents within a specified radius from a center point.
To find documents within a 50 km radius from a specific point:
1GeoFirePoint center = geo.point(latitude: 12.960632, longitude: 77.641603); 2var collectionReference = _firestore.collection('locations'); 3 4double radius = 50; 5String field = 'position'; 6 7Stream<List<DocumentSnapshot>> stream = geo.collection(collectionRef: collectionReference) 8 .within(center: center, radius: radius, field: field); 9 10stream.listen((List<DocumentSnapshot> documentList) { 11 documentList.forEach((DocumentSnapshot document) { 12 print(document.data()); 13 }); 14});
This code sets up a real-time stream of documents within the specified radius, updating as data changes in Firestore.
Geoflutterfire2 can handle collections with millions of documents by scoping geoqueries to a subset of data:
1var queryRef = _firestore.collection('locations').where('city', isEqualTo: 'bangalore'); 2var stream = geo.collection(collectionRef: queryRef) 3 .within(center: center, radius: radius, field: 'position');
Ensure your Firestore security rules are configured to allow reading and writing of location data. For example:
1service cloud.firestore { 2 match /databases/{database}/documents { 3 match /locations/{location} { 4 allow read, write: if true; 5 } 6 } 7}
Geoflutterfire2 is a powerful tool for any Flutter developer looking to implement location-based features in their app. It allows you to store and query Firestore documents based on geographic location efficiently. By following the steps outlined in this blog, you can easily set up and use Geoflutterfire2 in your Flutter projects.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.