imperiumlabs / GeoFirestore-iOS

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

Incorrect Results #12

Open raginggoat opened 5 years ago

raginggoat commented 5 years ago

I am using GeoFirestore to run geo queries against my Firebase Firestore. It seems to generally work but is returning more results than it should once I test with a radius of more than 25 miles. The closest location in my Firestore is 103 miles from the location I'm testing with. I've tested in 25 mile increments. 25 miles doesn't return any locations, 50 miles returns the closest one, 75 returns the closest one, and 100 miles returns all three of my test locations. Any idea what I'm doing wrong?

func getFacilites() {
        self.facilities = [[String: Any]]()

        let currentLatitude = self.currentLocation.coordinate.latitude
        let currentLongitude = self.currentLocation.coordinate.longitude

        let geoFireStore = Constants.geoFirestore
        let circleQuery = geoFireStore.query(withCenter: GeoPoint(latitude: currentLatitude, longitude: currentLongitude), radius: milesToKilometers())
        let _ = circleQuery.observeReady {
            print("All initial data has been loaded and events have been fired!")
            if self.facilities?.count == 0 {
                // ----------------------------------------------------------------
                // Alert user that no results were found with their search distance
                // ----------------------------------------------------------------
            }
        }
        let _ = circleQuery.observe(.documentEntered, with: { (key, location) in
            if let key = key {
                Constants.firestore.collection("facilities").document(key).getDocument { (document, error) in
                    if let document = document, document.exists {
                        self.facilities?.append(document.data()!)
                        self.addFaciltiesToMap()
                    } else {
                        print("Document does not exist.")
                    }
                }
            }
        })
    }
func milesToKilometers() -> Double {
        return self.searchDistance * 1.60934
    }
erikmath2705 commented 5 years ago

I am facing somewhat the same problem, where .documentEntered is called much more often than it should, and .documentExited is never called.

Did you find a solution?

avinashtreddy commented 4 years ago

I have observed that the calculation of distance doesn't appear to be correct. I have two points A and B which are located 1.08km apart (based on distance calculation from https://www.movable-type.co.uk/scripts/latlong.html); I have also double verified the CLLocation.distance() function also returns the same value of 1.08km as above between A and B. Based on this I am concluding that the distance between the points A and B are 1.08km.

The setLocation function will return a document at Position A.

I then create the following query: let query = geoFirestore.query(withCenter: positionB, radius: 1.0)

In the above scenario, since the distance between A and B is 1.08km (>1.0km), the documentEntered function should NOT enter. PROBLEM: The documentEntered function is entered. I then changed the radius = 0.5km however the documentEntered function enters again. I kept trying different values and noticed that when radius = 0.4863km, the documentEntered function DOES NOT enter, however radius = 0.4864km the documentEntered function is entered.

Can you please let me know why is this happening? How are you calculating the distance between two lat/long pairs?

MaxiTalenti commented 4 years ago

I have observed that the calculation of distance doesn't appear to be correct. I have two points A and B which are located 1.08km apart (based on distance calculation from https://www.movable-type.co.uk/scripts/latlong.html); I have also double verified the CLLocation.distance() function also returns the same value of 1.08km as above between A and B. Based on this I am concluding that the distance between the points A and B are 1.08km.

The setLocation function will return a document at Position A.

I then create the following query: let query = geoFirestore.query(withCenter: positionB, radius: 1.0)

In the above scenario, since the distance between A and B is 1.08km (>1.0km), the documentEntered function should NOT enter. PROBLEM: The documentEntered function is entered. I then changed the radius = 0.5km however the documentEntered function enters again. I kept trying different values and noticed that when radius = 0.4863km, the documentEntered function DOES NOT enter, however radius = 0.4864km the documentEntered function is entered.

Can you please let me know why is this happening? How are you calculating the distance between two lat/long pairs?

Any solution?

avinashtreddy commented 4 years ago

I ended up not using GeoFirestore-iOS due to this issue.