Skip to content

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.

Notifications You must be signed in to change notification settings

JobGetabu/DroidGeoFire

Repository files navigation

DroidGeoFire

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'
}

Usage in your Firestore Query

To Set Location Value in your document

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
	}

To Retrive the List of items from center location with the given radius

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

Listen continues data with real-time changes

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
}

OR

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
}

Listen data for only once

geoQuery.get()
	.addOnCompleteListener { firebaseFirestoreException, addedList, modifiedList, removedList   ->
		...
		//Do your stuff here
	}

OR

geoQuery.get()
	.addOnCompleteListener { firebaseFirestoreException, addedOrModifiedDataList, removedList  ->
		...
		//Do your stuff here
	}

OR

geoQuery.get()
	.addOnSuccessListener { addedOrModifiedDataList, removedList  ->
		...
		//Do your stuff here
	}
	.addOnFailureListener { exception ->
		//If any exception occured
	}

Support me by Buying me a coffee

Buy Me A Coffee

About

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.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published