skip to Main Content

I have tried to install GeoFire in Xcode/Swift.

According to the documentation (https://firebase.google.com/docs/firestore/solutions/geoqueries), this is done by adding "GeoFire/Utils" to Podfile. However, once installed I see errors. I’m suspecting this is caused from incompatibility with Firebase 10.23.1?

Why is this not available from Swift Package Manager?
Is "GeoFire/Utils" not maintained any more?
Are there any alternatives?

2

Answers


  1. In recent releases it is no longer necessary to use geohashes and geofire to perform geoqueries on Firestore. Instead you can perform range conditions on the latitude and longitude values in your documents.

    I just created an example this over the weekend in JavaScript that you can find here: https://stackblitz.com/edit/geofire-geoquery-2024. The important code from there:

    const query = collection
      .where("longitude", ">=", center.lng - range.lon)
      .where("longitude", "<=", center.lng + range.lon)
      .where("latitude", ">=", center.lat - range.lat)
      .where("latitude", "<=", center.lat + range.lat);
    

    So this is a query on the longitude and latitude fields in my documents, which describe the location of the document. This type of query only recently became possible on Firestore, and is documented in query with range and inequality filters on multiple fields and optimize these queries.

    The variables use in the code are:

    const MAX_DISTANCE_IN_KM = 250;
    const center = {
      lat:   38.555605,
      lng: -121.468926
    };
    const range = {
      lat: 1.0*MAX_DISTANCE_IN_KM / 110.574,
      lon: 1.0*MAX_DISTANCE_IN_KM / (111.320*Math.cos((center.lat * Math.PI) / (180)))
    };
    

    You’ll have to translate this code to Swift for your use-case, but after that you’ll no longer need the geofire library.

    Login or Signup to reply.
  2. Here is Frank’s code in Swift

    func maxDistanceToLoc(center: CLLocationCoordinate2D, maxDistanceKm: Double) -> CLLocationCoordinate2D {
        return CLLocationCoordinate2D(
            latitude: maxDistanceKm / 110.574,
            longitude: maxDistanceKm / (111.320 * cos(center.latitude * .pi / 180))
        )
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search