1

I have seen here that you can run a nearby geoquery in Firestore.

I am using the code from Ryan Lee, but the code does return the point in the range of latitude or longitude and not both depending of the what "whereField" I put before.

Also some other people are having the same problem, how do I solve it?

func getDocumentNearBy(latitude: Double, longitude: Double, distance: Double) {

// ~1 mile of lat and lon in degrees
let lat = 0.0144927536231884
let lon = 0.0181818181818182

let lowerLat = latitude - (lat * distance)
let lowerLon = longitude - (lon * distance)

let greaterLat = latitude + (lat * distance)
let greaterLon = longitude + (lon * distance)

let lesserGeopoint = GeoPoint(latitude: lowerLat, longitude: lowerLon)
let greaterGeopoint = GeoPoint(latitude: greaterLat, longitude: greaterLon)

let docRef = Firestore.firestore().collection("locations")
let query = docRef.whereField("location", isGreaterThan: lesserGeopoint).whereField("location", isLessThan: greaterGeopoint)

query.getDocuments { snapshot, error in
    if let error = error {
        print("Error getting documents: \(error)")
    } else {
        for document in snapshot!.documents {
            print("\(document.documentID) => \(document.data())")
        }
    }
}

}

func run() {
    // Get all locations within 10 miles of Google Headquarters
    getDocumentNearBy(latitude: 37.422000, longitude: -122.084057, distance: 10)
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Calonca
  • 125
  • 1
  • 7

0 Answers0