This project is created to give support to search geo-location based items using firestore's native query which also supports pagination and all the kind of condition which firestore query does.
Step 1. Add the JitPack repository to your root (project level) build.gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.JobGetabu:DroidGeoFire:latest-version'
}
val db = FirebaseFirestore.getInstance()
val document = db.collection("users").document("abc")
document.set(data)
.addOnSuccessListener{ _ ->
//Set Location After your document created on firestore db
document.setLocation(latitude, longitude, fieldName)
//fieldName is optional, if you will not pass it will set location in default field named "g"
//Also will add field named "geoLocation" as GeoPoint including latitude and longitude
//to count distance when querying the data
}
.addOnFailureListener { exception ->
//Document write failed
}
val db = FirebaseFirestore.getInstance()
/*Create two object to pass location and distance for radius*/
val centerLocation = Location(centerLatitude, centerLongitude)
val distanceForRadius = Distance(1.0, DistanceUnit.KILOMETERS)
// or you can set unit as DistanceUnit.MILES if you want to find for 1 mile
val geoQuery = GeoQuery()
.collection("users")
.whereEqualTo("status","approved")
.whereEqualTo("country","Kenya")
.whereIn("counties", arrayListOf("Kisii","Meru"))
.whereNearToLocation(centerLocation, distanceForRadius, fieldName)
//fieldName if you have passed at time of setLocation else it will take default as "g" if you do not pass
.startAfter(lastDocument) //optional (for pagination)
.limit(10) // If you requires only 10 data per query
geoQuery.addSnapshotListener { firebaseFirestoreException, addedList, modifiedList, removedList ->
...
//Do your stuff here
//If exception occurs, firebaseFirestoreException will not be null else
//In addedList, you will get all data that falls within distance and given query
//In modifiedList, you will get all data that falls within distance and given query, with changes
//In removedList, you will get data which is not relevent to your query or out of distance
}
geoQuery.addSnapshotListener { firebaseFirestoreException, addedOrModifiedDataList, removedList ->
...
//Do your stuff here
//If exception occurs, firebaseFirestoreException will not be null else
//In addedOrModifiedDataList, you will get all data that falls within distance and given query
//as either ADDED for first time in query or has MODIFIED as was in the query from first.
//In removedList, you will get data which is not relevent to your query or out of distance
}
geoQuery.get()
.addOnCompleteListener { firebaseFirestoreException, addedList, modifiedList, removedList ->
...
//Do your stuff here
}
geoQuery.get()
.addOnCompleteListener { firebaseFirestoreException, addedOrModifiedDataList, removedList ->
...
//Do your stuff here
}
geoQuery.get()
.addOnSuccessListener { addedOrModifiedDataList, removedList ->
...
//Do your stuff here
}
.addOnFailureListener { exception ->
//If any exception occured
}