imperiumlabs / GeoFirestore-iOS

GeoFirestore for iOS - Realtime location queries with Firestore
MIT License
127 stars 46 forks source link

Query all documents given coordinate and radius and understanding GFGeoHashQuery. #7

Open levieggert opened 5 years ago

levieggert commented 5 years ago

Hey first off, thanks a lot for this library. It is very handy.

I'm trying to better understand how to use GFGeoHashQuery in order to do my own querying against Firestore.

First, I'll run through what I am trying to accomplish and provide a code sample. Then, I will follow up with some questions.

  1. What I am trying to accomplish is, given a coordinate and radius, query all documents in Firestore that are within the radius of the given coordinate. I only need to query Firestore once because I will grab the user's current location and never ask for it again until the app is re-launched.

Here is some sample code for this.

let coordinate: CLLocationCoordinate2D = some-coordinate
let metersToMile: Double = 1609.34
let miles: Double = 50
let meters: Double = metersToMile * miles

let geoHashQueries: Set<AnyHashable> = GFGeoHashQuery.queries(forLocation: coordinate, radius: meters)
let geoHashQueriesArray: [AnyHashable] = Array(geoHashQueries) 

if let queryHash = geoHashQueriesArray.first as? GFGeoHashQuery
{
    let query: Query = self.eventsCollection.order(by: "g").whereField("g", isGreaterThanOrEqualTo: queryHash.startValue).whereField("g", isLessThanOrEqualTo: queryHash.endValue)

    query.getDocuments { (snapshot: QuerySnapshot?, error: Error?) in

        //map snapshot documents
    }
}

The main thing I notice is GFGeoHashQuery.queries(forLocation: coordinate, radius: meters) can return an array of geo hash queries with 0 or more objects.

I appreciate any feedback you can give me. After looking through your code in GeoFirestore.swift I feel like I am on the right track here. Also what are your thoughts on implementing something similar to this in GeoFirestore?

Thanks

theonlynick0430 commented 5 years ago

I do not believe that you need to use your own code for this. Just query Firestore and stop the query after the initial fetch.

On Sun, Sep 23, 2018 at 7:58 AM levieggert notifications@github.com wrote:

Hey first off, thanks a lot for this library. It is very handy.

I'm trying to better understand how to use GFGeoHashQuery in order to do my own querying against Firestore.

First, I'll run through what I am trying to accomplish and provide a code sample. Then, I will follow up with some questions.

  1. What I am trying to accomplish is, given a coordinate and radius, query all documents in Firestore that are within the radius of the given coordinate. I only need to query Firestore once because I will grab the user's current location and never ask for it again until the app is re-launched.

Here is some sample code for this.

let coordinate: CLLocationCoordinate2D = some-coordinatelet metersToMile: Double = 1609.34let miles: Double = 50let meters: Double = metersToMile * miles let geoHashQueries: Set = GFGeoHashQuery.queries(forLocation: coordinate, radius: meters)let geoHashQueriesArray: [AnyHashable] = Array(geoHashQueries) if let queryHash = geoHashQueriesArray.first as? GFGeoHashQuery { let query: Query = self.eventsCollection.order(by: "g").whereField("g", isGreaterThanOrEqualTo: queryHash.startValue).whereField("g", isLessThanOrEqualTo: queryHash.endValue)

query.getDocuments { (snapshot: QuerySnapshot?, error: Error?) in

    //map snapshot documents    }

}

The main thing I notice is GFGeoHashQuery.queries(forLocation: coordinate, radius: meters) can return an array of geo hash queries with 0 or more objects.

-

Shouldn't it only return 1 geo hash?

If there are multiple geo hash objects, do I have to query against Firestore with each geo hash?

Can multiple geo hash objects return results from Firestore?

I appreciate any feedback you can give me. After looking through your code Geofirestore.swift I feel like I am on the right track here. Also what are your thoughts on implementing something similar to this in GeoFirestore?

Thanks

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/imperiumlabs/GeoFirestore-iOS/issues/7, or mute the thread https://github.com/notifications/unsubscribe-auth/AaSzNuEvityrKJAXjbB7G57JvZRrc73Uks5ud6GBgaJpZM4W1u-N .

donpironet commented 5 years ago

I also have this problem. I just want to have all the documents that are in the radius. Now the problem I see is 1 that I don't see in the observeNext that I have all the documents and 2 I need to perform an extra query for getting the document?

vasquezdennisalon commented 4 years ago

Has anyone been able to solve this problem?