Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

ios - Once coordinates are saved as String in Firebase, how are they fetched in CLLLoationDegrees to calculate the distance between users?

Here is how I think it should be done:

1 Make a snapshot of all users’ coordinate Longitude and Latitude entries in Firebase - Print those out in output box to check this step.

2.1 Name a func for the purpose of calculating the distance between the logged in user and all other users

2.2 Inside that function make let coordinate? = the logged in user’s coordinates. The database has the String Value of the Coordinates so somehow this needs to be made to CLLLoationDegrees.

2.3 Make a loop for coordinate1 until coordinateX (ie all users in the database) for their coordinates

2.4 Use let distanceInMeters = coordinate?.distance(from: coordinate?) and loop for all distances until coordinateX (last one in database)

2.5 Print all the distances in output box. (I don’t need them printed on the app, I am simply getting the distances so that I can rank users by distance)

The Database looks like this for each kid:
   "people" : {
    "2jWm6rUfZVYAGkSST0fvVvwolY92" : {
      "Coordinates" : {
        "latitude" : 45.785834,
        "longitude" : -100.406417
      },
      "Education" : "BA Arts",
      "PhotoPosts" : "https://firebasestorage.googleapis.com/v0/b/westpluto1.appspot.com/o/images%2FPhotoPosts?alt=media&token=ccd5a139-b3e1-4ac9-965c-2f09c149fc74",
      "users" : "jackmaster1091@aol.com"
    },


///Part A
  var refArtists : DatabaseReference!
var people = [Userx]()
var latestLocation: [String: Double]?   ////this double was added to get coordinates into firebase under longitude and latitude

///Part B

   let thisUsersUid = Auth.auth().currentUser?.uid //Mr. Dunn's uid
refArtists = Database.database().reference().child("people");

refArtists.observe(DataEventType.value,  with: {snapshot in
if snapshot.childrenCount>0{
self.people.removeAll()
   for people in snapshot.children.allObjects as! [DataSnapshot] {
   if people.key != thisUsersUid { //do not add this users info to the array
   let peopleObject = people.value as? [String: AnyObject]
   let peopleCoordinates = peopleObject?["Coordinates"]  as? String
   let peopl = Userx(Coordinates: peopleCoordinates)
   self.people.append(peopl)  
        }
        self.table.reloadData()
        print(snapshot)
        }
    }

})

///Part C

func distance (){
    let databaseRef = Database.database().reference()
    let uid = Auth.auth().currentUser!.uid
    let coordinate? = CLLocation(latitude: databaseRef.child("people").child(uid).child("Coordinates").child("latitude"), longitude: databaseRef.child("people").child(uid).child("Coordinates").child("longitude")) //////I think this is incorrect even w/o string to CLL conversion because I don’t think longitude and latitude and technically children of Coordinates
        …..
        let distanceInMeters = coordinate?.distance(from: coordinate?)
        let distanceInMeters2 = coordinate?.distance(from: coordinate2)
        ….
Print …
Print …

///////Code based on answer from Jay

 let uid = Auth.auth().currentUser!.uid

    let ref = Database.database().reference().child("people").child(uid).child("Coordinates")

ref.observeSingleEvent(of: .value, with: { snapshot in
    let allCoordinates = snapshot.children.allObjects as! [DataSnapshot]
    for Coordinates in allCoordinates {
        let lat = Coordinates.childSnapshot(forPath: "latitude").value as! CLLocationDegrees
        let lon = Coordinates.childSnapshot(forPath: "longitude").value as! CLLocationDegrees
        let userId = Coordinates.key
        let locCoord = CLLocationCoordinate2DMake(lat, lon)
        let bcoordinates  = CLLocationCoordinate2D(latitude: lat, longitude: lon)
    }
})

///2nd updated code:

    let uid = Auth.auth().currentUser!.uid

refArtists = Database.database().reference().child("people");

let ref = self.refArtists.child(uid).child("Coordinates")


ref.observeSingleEvent(of: .value, with: { snapshot in
    let allLocations = snapshot.children.allObjects as! [DataSnapshot]
    for location in allLocations {
        let userId = location.key
        let coordSnap = location.childSnapshot(forPath: "Coordinates")
        let lat = coordSnap.childSnapshot(forPath: "latitude").value as! CLLocationDegrees
        let lon = coordSnap.childSnapshot(forPath: "longitude").value as! CLLocationDegrees
        let locCoord = CLLocationCoordinate2DMake(lat, lon)
        let coordinates  = CLLocationCoordinate2D(latitude: lat, longitude: lon)
        print("   coords: (lat)  (lon)")
    }
})

////This is the code that now gives two coords printed:

let ref = self.refArtists.child(uid).child("Coordinates")
ref.observeSingleEvent(of: .value, with: { snapshot in
    let allLocations = snapshot.children.allObjects as! [DataSnapshot]
    for location in allLocations {
        let userId = location.key
        let lat = snapshot.childSnapshot(forPath: "latitude").value as! CLLocationDegrees
        let lon = snapshot.childSnapshot(forPath: "longitude").value as! CLLocationDegrees
        let locCoord = CLLocationCoordinate2DMake(lat, lon)
        let coordinates  = CLLocationCoordinate2D(latitude: lat, longitude: lon)
        print("   coords: (lat)  (lon)")
    }

I expect to rank the users based on location after all this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...