1
1
package com.ckdroid.geofirequery
2
2
3
+ import android.location.Location
3
4
import com.ckdroid.geofirequery.model.Distance
4
- import com.ckdroid.geofirequery.model.QueryLocation
5
5
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
7
7
import com.google.android.gms.tasks.Task
8
8
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
10
11
import com.google.firebase.firestore.SetOptions
11
12
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
12
19
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
23
25
}
24
26
25
27
fun DocumentReference.setLocation (
@@ -31,5 +33,6 @@ fun DocumentReference.setLocation(
31
33
val degreeMatch = (latitude + 90 ) * 180 + longitude
32
34
val mapData: HashMap <String , Any > = hashMapOf()
33
35
mapData[fieldName] = degreeMatch
36
+ mapData[KEY_GEO_LOCATION ] = GeoPoint (latitude, longitude)
34
37
return if (useUpdateMethod) this .update(mapData) else this .set(mapData, SetOptions .merge())
35
38
}
0 commit comments