2016-02-09 12 views
5

Sono abbastanza nuovo per lavorare con Firebase e la sua libreria di ubicazioni, GeoFire. Attualmente sto affrontando qualche problema durante la strutturazione dei miei dati.Query GeoFire sulla posizione dell'utente

Al momento il mio db è qualcosa di simile a questo:.

users 
    facebook:xxxxx 
    displayName: xx 
    firstName: xx 
    lastName: xx 
    location: 
     g: xxxx 
     l: 
     0: xxx 
     1: xxx 
    facebook:yyyyy 
    displayName: yy 
    firstName: yy 
    lastName: yy 
    location: 
     g: yyyy 
     l: 
     0: yyy 
     1: yyy 

Vorrei interrogare sugli utenti che si trovano vicino alla mia dell'utente attualmente collegato Per farlo non riesco a capire quale strada devo specificare.

Al momento sto facendo in questo modo (ma non funziona):

risparmio posizione attuale

let root = Firebase(url: "myapp.firebaseio.com") 
let usersRoot = root.childByAppendingPath("users") 
geoFire = GeoFire(firebaseRef: usersRoot.childByAppendingPath(root.authData.uid)) 

geoFire.setLocation(currentLocation, forKey: "location") { (error) in 
    if (error != nil) { 
     print("An error occured: \(error)") 
    } else { 
     print("Saved location successfully!") 
    } 
} 

recupero di altri utenti nelle vicinanze

let geoFire = GeoFire(firebaseRef: Firebase(url: "myapp.firebaseio.com").childByAppendingPath("users")) 
let query = geoFire.queryAtLocation(currentLocation, withRadius: radius) 

query.observeEventType(GFEventTypeKeyEntered, withBlock: { 
    (key: String!, location: CLLocation!) in 

    print("+ + + + Key '\(key)' entered the search area and is at location '\(location)'") 
    self.userCount++ 
    self.refreshUI() 
}) 

UPDATE

risparmio posizione attuale

let root = Firebase(url: "myapp.firebaseio.com") 
geoFire = GeoFire(firebaseRef: root.childByAppendingPath("locations")) 

geoFire.setLocation(currentLocation, forKey: root.authData.uid) { (error) in 
    if (error != nil) { 
     print("An error occured: \(error)") 
    } else { 
     print("Saved location successfully!") 
    } 
} 

recupero di altri utenti vicino

let geoFire = GeoFire(firebaseRef: Firebase(url: "myapp.firebaseio.com").childByAppendingPath("locations")) 
let query = geoFire.queryAtLocation(currentLocation, withRadius: radius) 

query.observeEventType(GFEventTypeKeyEntered, withBlock: { 
    (key: String!, location: CLLocation!) in 

    print("+ + + + Key '\(key)' entered the search area and is at location '\(location)'") 
    self.userCount++ 
    self.refreshUI() 
}) 

risposta

4

Per GeoFire si devono tenere un segmento in l'albero che contiene solo la geolocalizzazione e poi si avere un altro segmento nell'albero che contiene le altre informazioni sugli oggetti.

user_locations 
    uid1: 
    g: xxxx 
    l: 
     0: xxx 
     1: xxx 
    uid2: 
    g: xxxx 
    l: 
     0: xxx 
     1: xxx 
user_profiles: 
    uid1: 
    name: "giacavicchioli" 
    uid2: 
    name: "Frank van Puffelen" 

Vedi anche: Query firebase data linked to GeoFire

+0

Grazie! Ci sto provando in questo momento, ma ancora non funziona. Ho cercato in questo modo (considerando la vostra struttura db): lasciare geoFire = GeoFire (firebaseRef: root.childByAppendingPath ("user_locations")) interrogazione let = geoFire.queryAtLocation (CurrentLocation, withRadius: 10) query.observeEventType (GFEventTypeKeyEntered, withBlock: { (chiave: stringa !, posizione: CLLocation!) in stampa ("+ + + + Tasto '\ (chiave)' è entrato nell'area di ricerca ed è in posizione '\ (posizione)'") }) – giacavicchioli

+0

Risolto, si è verificato un problema durante l'impostazione del centro query. Grazie. – giacavicchioli

+0

Fireofase geofire-js: come ottenere l'elenco delle chiavi di vicino da posizioni statiche (fisse): http://stackoverflow.com/questions/43632746/firebase-geofire-js-how-to-get-list-of-keys -di-vicino-by-staticfixed-locations –

1

Per salvare tutti i record che soddisfano la query è necessario memorizzare tutti usando qualcosa come:

var allKeys = [String:CLLocation]() 

    circleQuery.observeEventType(GFEventTypeKeyEntered, withBlock: { 
     (key: String!, location: CLLocation!) in 

     allKeys [key]=location 
     } 

e quindi utilizzare

circleQuery.observeReadyWithBlock({ 
     print("All initial data has been loaded and events have been fired!") 
    }) 
0

Sembra che ci sia un problema durante l'impostazione di GFEventType. Che verrà risolto a quanto pare sulla prossima versione di GeoFire 1.3, per ora si può fare questo ...

let geoFire = GeoFire(firebaseRef: ref) 

var allKeys = [String:CLLocation]() 

let query = geoFire.queryAtLocation(coordinates, withRadius: 0.2) 
print("about to query") 
query.observeEventType(GFEventType.init(0), withBlock: {(key: String!, location: CLLocation!) in 
    print("Key: \(key), location \(location.coordinate.latitude)") 
    allKeys [key] = location 
}) 

Come si può vedere GFEventType.init (0) ... che mappe per i tre tipi di enumerazioni che non sono mappate correttamente su GeoFire in Swift per qualche motivo.

Problemi correlati