2016-09-27 8 views
1

GeoFireを使用してFirebaseデータベースに座標を保存しようとしています。GeoFire Swift 3 - 座標の保存と更新

新しい座標を毎秒変更/更新するため、新しい座標を更新する方法が不明です。 childByAutoIdでは、各バイク用に新しい一意のIDを生成しています。

この固有のバイクIDはどのように参照しますか?たとえば、ユーザはFIRAuth.auth()?.currentUser?.uidによって呼び出されます。これは可能ですか?

let geofireRef = FIRDatabase.database().reference().child("Bike").childByAutoId() 
let geoFire = GeoFire(firebaseRef: geofireRef) 

var data = geoFire?.setLocation(CLLocation(latitude: userIncrementLat, longitude: userIncrementLong), forKey: "BikeId") 

マイFirebaseデータベース構造は次のようになります...

Root 
1. Bike 
2. UniqueUID Number (Firebase) 
3. BikeId 
4. g 
     l 
5.  0: 
     1: 
+0

お役に立てば幸いですか!私はこのユニークなIDと呼ぶ方法を理解するのに苦労しています。 –

答えて

6

これは、時間によって更新ユーザーの場所のための私のFirebase DB構造であり、地図上に表示するために近くのユーザーをretrive:

db-root 
"users" : { 
    <userUID> : { 
     "someKey" : "someValue", 
     ... 
    } 
} 
"users_location" : { 
    <userUID> : { 
     <geofireData> ... 
    } 
} 

バールを使用する:

let ref = FIRDatabase.database().reference() 
let geoFire = GeoFire(firebaseRef: ref.child("users_location")) 
私は findNearbyUsers機能を使用する近くのユーザーを検索するに

func updateUserLocation() { 

    if let myLocation = myLocation { 

     let userID = FIRAuth.auth()!.currentUser!.uid 
     geoFire!.setLocation(myLocation, forKey: userID) { (error) in 
      if (error != nil) { 
       debugPrint("An error occured: \(error)") 
      } else { 
       print("Saved location successfully!") 
      } 
     } 

    } 
} 

:ログインしているユーザーの場所を更新するには

。近くのユーザーを見つけ、nearbyUsersのユーザーのUIDキーを保存すると便利です。 observeReady関数はクエリの完了後に実行され、ユーザーの詳細を取得するためにUIDを使用します(マップ上にユーザーの詳細を表示するためにこれを使用します)。

func findNearbyUsers() { 

    if let myLocation = myLocation { 

     let theGeoFire = GeoFire(firebaseRef: ref.child("users_location")) 
     let circleQuery = theGeoFire!.query(at: myLocation, withRadius: radiusInMeters/1000) 

     _ = circleQuery!.observe(.keyEntered, with: { (key, location) in 

      if !self.nearbyUsers.contains(key!) && key! != FIRAuth.auth()!.currentUser!.uid { 
       self.nearbyUsers.append(key!) 
      } 

     }) 

     //Execute this code once GeoFire completes the query! 
     circleQuery?.observeReady({ 

      for user in self.nearbyUsers { 

       self.ref.child("users/\(user)").observe(.value, with: { snapshot in 
        let value = snapshot.value as? NSDictionary 
        print(value) 
       }) 
      } 

     }) 

    } 

} 

は、それが値を取得することだろうか。また

+0

こんにちは、ユーザーの詳細ではなくユーザーを表示する場合はどうすればいいですか?ありがとう – John

関連する問題