I am using streambuilder to fetch documents from firestore and pass individual data to listTile . I want to put a condition to fetch only equipments within 10 km range. In my database , the location is in the format of geopoint [latitute,longitude]. I used geoquery to restrict the equipments within particular range. But I got to know that it only fetches based on latitude and not longitude. How to run a geo "nearby" query with firestore? - from this answer it is clear that we have to use geohashes for this query. But I am not sure exactly where to use it in my code. Below is my code
Expanded(
child: TabBarView(
children: _site == BestTutorSite.sell
? [
equipmentListStreamBuilder(
'SellEquipments', 'equipmentType.Tractor'),
equipmentListStreamBuilder(
'SellEquipments', 'equipmentType.Implement'),
equipmentListStreamBuilder(
'SellEquipments', 'equipmentType.Harvester'),
]
: [
equipmentListStreamBuilder(
'RentEquipments', 'equipmentType.Tractor'),
equipmentListStreamBuilder(
'RentEquipments', 'equipmentType.Implement'),
equipmentListStreamBuilder(
'RentEquipments', 'equipmentType.Harvester'),
]),)
StreamBuilder<QuerySnapshot<Object?>> equipmentListStreamBuilder(
collectionName, eType) {
return StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection(collectionName)
.where("equipmentType", isEqualTo: eType)
.where("geopoint", isLessThan: greaterGeopoint)
.where("geopoint", isGreaterThan: lesserGeopoint)
.snapshots(),
builder: (context, snapshot) {
return !snapshot.hasData
? Center(child: CircularProgressIndicator())
: ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) {
DocumentSnapshot data = snapshot.data!.docs[index];
return ListTile(
leading: Icon(Icons.car_rental),
title: Text(data['brand'].toString()),
subtitle: Text("prem"),
trailing: Icon(Icons.more_vert),
);
},
);
},
);
getNearByEquipments(double latitude, double longitude, double distance) {
print("---------inside getequipments");
// ~1 mile of lat and lon in degrees
double lat = 0.0144927536231884;
double lon = 0.0181818181818182;
double lowerLat = latitude - (lat * distance);
double lowerLon = longitude - (lon * distance);
double greaterLat = latitude + (lat * distance);
double greaterLon = longitude + (lon * distance);
setState(() {
lesserGeopoint = GeoPoint(lowerLat, lowerLon);
greaterGeopoint = GeoPoint(greaterLat, greaterLon);
});
}