2016-02-09 11 views
5

Jestem całkiem nowy w pracy z Firebase i jego biblioteką lokalizacji, GeoFire. Obecnie napotykam pewien problem podczas strukturyzacji moich danych.Zapytanie GeoFire na temat lokalizacji użytkownika

W tej chwili mój db jest coś takiego:.

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 

Chciałbym zapytać użytkowników, które są w pobliżu mojego bieżącego zalogowanego użytkownika Aby to zrobić nie mogę zrozumieć, co mam na tor sprecyzować.

Obecnie robię tak (ale nie działa):

zapisywania aktualnej lokalizacji

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!") 
    } 
} 

przy pobieraniu innych użytkowników w pobliżu

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

oszczędność bieżąca lokalizacja

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!") 
    } 
} 

pobierania inni użytkownicy pobliżu

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() 
}) 

Odpowiedz

4

Dla GeoFire trzeba zachować segment w drzewo, które zawiera tylko geolokalizacji, a potem mają inny segment w drzewie, który zawiera inne informacje o przedmiotach.

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" 

zobacz także: Query firebase data linked to GeoFire

+0

Dzięki! Próbuję teraz, ale nadal nie działa. Próbowałem w ten sposób (biorąc pod uwagę strukturę db): niech geoFire = GeoFire (firebaseRef: root.childByAppendingPath ("user_locations")) let query = geoFire.queryAtLocation (currentLocation, withRadius: 10) query.observeEventType (GFEventTypeKeyEntered, withBlock: { (klucz: String !, location: CLLocation!) w print ("+ + + + klawisz" \ (klucz) "wprowadzono w obszar wyszukiwania i znajduje się w lokalizacji" \ (lokalizacja) "") }) – giacavicchioli

+0

Rozwiązano problem z ustawieniem centrum zapytań. Dzięki. – giacavicchioli

+0

Firebase geofire-js: jak uzyskać listę kluczy w pobliżu lokalizacji statycznych (stałych): http://stackoverflow.com/questions/43632746/firebase-geofire-js-how-to-get-list-of-keys -of-near-by-staticfixed-lokalizacje –

1

Aby zapisać wszystkie rekordy, które pasują do zapytania musisz przechowywać wszystkie użyciu coś jak:

var allKeys = [String:CLLocation]() 

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

     allKeys [key]=location 
     } 

a następnie użyć

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

Wygląda na to, że wystąpił problem podczas konfigurowania elementu GFEventType. Że zostaną rozwiązane wydaje na następnej wersji GeoFire 1.3, teraz można to zrobić ...

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 
}) 

Jak widać GFEventType.init (0) ... który mapuje do trzech typów wyliczenia, które z jakiegoś powodu nie są poprawnie odwzorowane w GeoFire w Swift.

Powiązane problemy