0

My coding (as you will see) and familiarity with Xcode and Swift is basic, but I'm learning... I realize my nesting of the code isn't the greatest, if good at all. I am trying to integrate GeoFire within my Xcode 10 app. I have looked for a clear explanation, but I am still yet to find an answer. I have a registrationForm to register users. The initial Firebase setup was straightforward, and I have a fully functional form that registers the user in Firebase. Now I am trying to add GeoFire in order to track the user's location.

I have tried a bunch of approaches from YouTube videos and websites, none of which work.

import UIKit
import Foundation
import CoreLocation
import Firebase
import GeoFire

class SecondViewController: UIViewController {

    //Firebase DB ref
    var refDriver: DatabaseReference!




    @IBOutlet weak var name: UITextField!

    @IBOutlet weak var employeenumber: UITextField!    

    @IBOutlet weak var label: UILabel!


    @IBAction func submit(_ sender: UIButton) {
        addDriver()
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        //configuring firebase and ensures no error is returned
        if(FirebaseApp.app() == nil){
            FirebaseApp.configure()

        }


        //getting a reference to the node driver
        refDriver = Database.database().reference().child("driver");

    }

    func addDriver(){
        //generating a new key inside driver node
        //and also getting the generated key
        let key = refDriver.childByAutoId().key

        //creating driver with the given values
        let driver = ["id":key,
                      "name": name.text! as String,
                        "employeenumber": employeenumber.text! as String
                            ]

        //adding the driver to Firebase
        refDriver.child(key!).setValue(driver)



        //displaying submit message
        label.text = "You are registered."
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

Di Nerd Apps
  • 770
  • 8
  • 15
Peter
  • 1
  • 1

1 Answers1

0

hey try importing the module after you install the pod

import GeoFire
**var geoFire: GeoFire?**
var myQuery: GFQuery?

in viewdidLoad() function call the setupGeofire()

   override func viewDidLoad() {
       super.viewDidLoad()
       // Do any additional setup after loading the view.
       setupGeoFire()
   }


func setupGeoFire() {

       let geoFireRef = Database.database().reference().child("user_locations")
       let geoFire = GeoFire(firebaseRef: geoFireRef)
       self.geoFire = geoFire

       loadUsersWithin(radius: 100)
   }

in setupGeoFire i made a var geoFireRef this is just the node you want to save your users location info

let geoFireRef = Database.database().reference().child("user_locations")

then we make the geoFire object by passing in that node location, our geoFireRef var

then just making the geoFire object accessible everywhere using ``` self.geoFire = geoFire`` `

if you want to get users in a specific radius (km) you can check the func below too

func loadUsersWithin(radius: Double){

        guard let uid = Auth.auth().currentUser?.uid else{return}
        geoFire?.getLocationForKey(uid, withCallback: { (userLocation, err) in

            guard let userLocation = userLocation else{return}
            self.myQuery = self.geoFire?.query(at: userLocation, withRadius: radius)

            self.myQuery?.observe(.keyEntered, with: { (key, location) in

                if key == uid{
                    return
                }
                Database.database().reference().child("users").child(key).observeSingleEvent(of: .value, with: { (snapshot) in

                    guard let userDictionary = snapshot.value as? [String:Any] else{return}
                    let user = User(uid: key, dictionary: userDictionary)
                    self.user = user
                    self.usersArray.append(user)
                    self.nearbyCV.reloadData()
                }, withCancel: { (err) in
                    print(err)
                })
            })
        })

    }
}

Here is my User.swift Data Model

import UIKit

class User{

    var name: String
    var userImgUrlString: String
    var age: String
    var uid: String
    var aboutMeText:String

    init(uid: String,dictionary: [String: Any]) {

        self.name = dictionary["username"] as? String ?? ""
        self.userImgUrlString = dictionary["profileImageUrlString"] as? String ?? ""
        self.aboutMeText = dictionary["aboutMe"] as? String ?? ""
        self.uid = uid
        self.age = dictionary["age"] as? String ?? ""
    }
}```
hope this helps yo it was hard to find info for me too when i just started but it gets better
Di Nerd Apps
  • 770
  • 8
  • 15
  • Yes, it certainly helps and is much better than I've been able to find. However, I now have 2 errors: let user = User(uid: key, (Argument passed to call that takes no arguments), and self.nearbyCV.reloadData() (Value of type 'SecondViewController' has no member 'nearbyCV') – Peter Sep 21 '19 at 17:16
  • @Peter User is a data model or Struct i used to hold the data so if you dont have a User.swift file then it wont know what it is. and neabyCV outlet for the UiCollectionView i am using. the ".reloadData()" recalls the numberOfRows function and othere delegate methods to repopulate the collectionView with new data – Di Nerd Apps Sep 21 '19 at 18:11