0

I'm using Swift to build an iOS to share locations. Is there anyone know How to use Geofire with Swift to store a location from one simulator and then show the stored location in another simulator?

Thanks a lot.

Sloan
  • 79
  • 7
  • please check this https://stackoverflow.com/questions/25296691/get-users-current-location-coordinates/41786406#41786406 – Lyju I Edwinson Nov 06 '17 at 03:39
  • Thanks for your answers. After trying implementing it, I found that Geofire is a bit buggy and seems not working well with Firebase. Xcode cannot find Geofile even after imported. Not sure if Geofire is good solution for storing coordinates. – Sloan Nov 06 '17 at 21:29
  • geofire is good, if you are using cocopods, please do a clean workspace and build again multiple times. if you tell me what is not working, i can help you. – Lyju I Edwinson Nov 07 '17 at 21:53
  • So grateful! The project just could not find the module. As you said, need to build again multiple times. I'm trying to use Firebase to store and retrieve geolocation info. Do you have any samples or examples to use Geofire to store and retrieve the location info? Thanks. – Sloan Nov 10 '17 at 19:48
  • Please install Geofire manually, cocopods way wasn't working for me. Ping me your email address and I will send steps. – Lyju I Edwinson Nov 11 '17 at 08:55
  • Thanks for your great help! Sounds like you recommend using Geofire, not Firebase to store and retrieve coordinates. I'm interested in giving it a try. I'm new to iOS development. Any examples would be highly appreciated. My email: patrick.shih1@gmail.com – Sloan Nov 11 '17 at 16:29

1 Answers1

5

First try using cocopods as below,

pod 'Firebase', '~>4.0'
pod 'Firebase/Auth'
pod 'Firebase/Database'
pod 'Firebase/Storage'
pod 'GeoFire', :git => 'https://github.com/firebase/geofire-objc.git'

Use the below code to set and get location from Geofire.

struct FndDatabase {
    static let LOCATION = Database.database().reference().child("userlocation")
    static let GEO_REF = GeoFire(firebaseRef: LOCATION)
}


func setUserLocation(location: CLLocation) {
    let user = Auth.auth().currentUser?.uid //userid for the logged in user
    if let uid = user {
        FndDatabase.GEO_REF?.setLocation(location, forKey: uid)
    }
}

func getLocation() -> CLLocation? {
    var loc: CLLocation?
    if let uid = Auth.auth().currentUser?.uid {
        FndDatabase.GEO_REF?.getLocationForKey(uid, withCallback: { (location, err) in
            if err != nil {
                print( "Error getting Userlocation from Geofire \(err.debugDescription)")
            } else {
                print( "UserLocation is available \(location.debugDescription)")
                loc = location
            }
        })

    }
    return loc
}

in case if you see this error 'FirebaseDatabase/FirebaseDatabase.h' file not found.

then do this, select FirebaseDatabase.framework from pods and link it to Geofire as shown in the image

image gefire with firebase

A sample code is uploaded to : https://github.com/ledwinson/firebasegeo

A sample in firebase after storing location is shown below.

Lyju I Edwinson
  • 1,764
  • 20
  • 20