1

I want to get current user location and then sort all other registered users by their locations to the current user.

When user registered I save his latitude and longitude in Firebase Firestore. My model:

import UIKit
import FirebaseFirestore

struct MUser: Hashable, Decodable {
    var username: String
    var email: String
    var avatarStringURL: String
    var description: String
    var sex: String
    var id: String
    var latitude: String
    var longitude: String
    
    init(username: String, email: String, avatarStringURL: String, description: String, sex: String, id: String, latitude: String, longitude: String) {
        self.username = username
        self.email = email
        self.avatarStringURL = avatarStringURL
        self.description = description
        self.sex = sex
        self.id = id
        self.latitude = latitude
        self.longitude = longitude
    }
    
    init?(document: DocumentSnapshot) {
        guard let data = document.data() else { return nil}
        guard let username = data["username"] as? String,
        let email = data["email"] as? String,
        let avatarStringURL = data["avatarStringURL"] as? String,
        let description = data["description"] as? String,
        let sex = data["sex"] as? String,
        let id = data["uid"] as? String,
        let latitude = data["latitude"] as? String,
        let longitude = data["longitude"] as? String
        
        else { return nil }
        
        self.username = username
        self.email = email
        self.avatarStringURL = avatarStringURL
        self.description = description
        self.sex = sex
        self.id = id
        self.latitude = latitude
        self.longitude = longitude
    }
    
    init?(document: QueryDocumentSnapshot) {
        let data = document.data()
        guard let username = data["username"] as? String,
        let email = data["email"] as? String,
        let avatarStringURL = data["avatarStringURL"] as? String,
        let description = data["description"] as? String,
        let sex = data["sex"] as? String,
        let id = data["uid"] as? String,
        let latitude = data["latitude"] as? String,
        let longitude = data["longitude"] as? String else { return nil }
        
        self.username = username
        self.email = email
        self.avatarStringURL = avatarStringURL
        self.description = description
        self.sex = sex
        self.id = id
        self.latitude = latitude
        self.longitude = longitude
    }
    
    var representation: [String: Any] {
        var rep = ["username": username]
        rep["email"] = email
        rep["avatarStringURL"] = avatarStringURL
        rep["description"] = description
        rep["sex"] = sex
        rep["uid"] = id
        rep["latitude"] = latitude
        rep["longitude"] = longitude
        return rep
    }
    
    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }
    
    static func == (lhs: MUser, rhs: MUser) -> Bool {
        return lhs.id == rhs.id
    }
    
    func contains(filter: String?) -> Bool {
        guard let filter = filter else { return true }
        if filter.isEmpty { return true }
        let lowercasedFilter = filter.lowercased()
        return username.lowercased().contains(lowercasedFilter)
    }
}

Then I want to query all user from the specific distance, for example 50km from current users from Firebase Firestore, and then sort all these users from closest to longest distance for the current user location.

But how I can make such query when I've only latitude and longitude values in Firebase Firestore?

And then how I can sort them?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Parcker
  • 219
  • 1
  • 10
  • https://stackoverflow.com/questions/42091994/how-to-query-nearest-users-in-firebase-with-swift ? – Larme Feb 24 '21 at 11:32
  • But I'm using Firebase Firestore, geofire requires Realtime Database. – Parcker Feb 24 '21 at 11:40
  • 1
    Have you read through the answers at this question: https://stackoverflow.com/questions/46630507/how-to-run-a-geo-nearby-query-with-firestore – DonMag Feb 24 '21 at 13:28
  • [GeoFirestore](https://github.com/imperiumlabs/Geofirestore-ios) is what you want for Firestore – Jay Feb 24 '21 at 19:07

0 Answers0