Skip to content

Commit badff2f

Browse files
authored
Merge pull request #9 from chintan369/feature_resolve_issues
Added Filter to Get Exact Accurate Data and Removed Extra Data Out of Distance
2 parents e7854be + 23fddf4 commit badff2f

File tree

10 files changed

+540
-14
lines changed

10 files changed

+540
-14
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/sonarIssues.xml

Lines changed: 100 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
package com.ckdroid.geofirequery
22

3+
import android.location.Location
34
import com.ckdroid.geofirequery.model.Distance
4-
import com.ckdroid.geofirequery.model.QueryLocation
55
import com.ckdroid.geofirequery.model.QueryLocation.Companion.DEFAULT_KEY_GEO_DEGREE_MATCH
6-
import com.ckdroid.geofirequery.utils.BoundingBoxUtils
6+
import com.ckdroid.geofirequery.model.QueryLocation.Companion.KEY_GEO_LOCATION
77
import com.google.android.gms.tasks.Task
88
import com.google.firebase.firestore.DocumentReference
9-
import com.google.firebase.firestore.Query
9+
import com.google.firebase.firestore.DocumentSnapshot
10+
import com.google.firebase.firestore.GeoPoint
1011
import com.google.firebase.firestore.SetOptions
1112

13+
internal fun DocumentSnapshot.isInGivenDistance(currentLocation: Location?, distance: Distance?): Boolean {
14+
val documentLocation: GeoPoint? = this.getGeoPoint(KEY_GEO_LOCATION)
15+
if (documentLocation != null) {
16+
val location = Location("")
17+
location.latitude = documentLocation.latitude
18+
location.longitude = documentLocation.longitude
1219

13-
fun Query.whereNearToLocation(
14-
queryAtLocation: QueryLocation,
15-
distance: Distance,
16-
fieldName: String = DEFAULT_KEY_GEO_DEGREE_MATCH
17-
): Query {
18-
val geoPointUtils = BoundingBoxUtils(distance.unit)
19-
val boundingBox = geoPointUtils.getBoundingBox(queryAtLocation, distance.distance)
20-
return orderBy(fieldName)
21-
.whereGreaterThanOrEqualTo(fieldName, boundingBox.minimumMatch)
22-
.whereLessThanOrEqualTo(fieldName, boundingBox.maximumMatch)
20+
val distanceInMeters = location.distanceTo(currentLocation).toDouble()
21+
22+
return distanceInMeters <= (distance?.distanceInMeters ?: 0.0)
23+
}
24+
return false
2325
}
2426

2527
fun DocumentReference.setLocation(
@@ -31,5 +33,6 @@ fun DocumentReference.setLocation(
3133
val degreeMatch = (latitude + 90) * 180 + longitude
3234
val mapData: HashMap<String, Any> = hashMapOf()
3335
mapData[fieldName] = degreeMatch
36+
mapData[KEY_GEO_LOCATION] = GeoPoint(latitude, longitude)
3437
return if (useUpdateMethod) this.update(mapData) else this.set(mapData, SetOptions.merge())
3538
}

0 commit comments

Comments
 (0)