Вопрос:

Representing Coordinates from Firebase as Annotations Swift

swift firebase annotations mapkit core-location

147 просмотра

1 ответ

17 Репутация автора

I am having trouble putting annotations on my map from longitudinal and latitudinal degrees I uploaded to firebase. I want to add an annotation for all of my users but cannot get xcode to recognize my userLatitude and userLongitude variables in another function.

Anything will help!

    func retrieveUsers(){
        let ref = Database.database().reference()
        ref.child("users").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
            let users = snapshot.value as! [String: AnyObject]
            self.user.removeAll()
            for (_,value) in users {
                if let uid = value["uid"] as? String {
                    if uid != Auth.auth().currentUser!.uid {
                        let userToShow = User()
                        if let fullName = value["full name"] as? String,
                        let imagePath = value["urlToImage"] as? String,
                        //String(format: "%f", self.currentUserLocation?.longitude ?? 0),
                        let userLongitude = value["long"] as? CLLocationDegrees,
                        let userLatitude = value["lat"] as? CLLocationDegrees
                        //why isnt it recognizing the users degrees

                        {
                            userToShow.fullName = value["full name"] as? String
                            userToShow.imagePath = value["urlToImage"] as? String
                            userToShow.userID = value["uid"] as? String
                            userToShow.userLongitude = value["long"] as? CLLocationDegrees
                            userToShow.userLatitude = value["lat"] as? CLLocationDegrees

                            self.user.append(userToShow)
                        }


                    }

                }
            }
            DispatchQueue.main.async {
                self.map.reloadInputViews()

                //not sure if this is right
            }
        })

    }




    func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {

        let otherUserLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(userLatitude, userLongitude)

        let userAnnotation = MKPointAnnotation()
        userAnnotation.coordinate = otherUserLocation
        userAnnotation.title = "fullName"





    }
Автор: Becca D Источник Размещён: 08.11.2017 10:17

Ответы (1)


0 плюса

148 Репутация автора

Решение

And if you change as? CLLocationDegrees by as? String and manage the cast in CLLocationDegrees afterwards?

Because I think, in Firebase, data is stored as String and not as Double (that is the type of data a CLLocationDegrees type is referring to).

Автор: schtipoun Размещён: 08.11.2017 10:25
Вопросы из категории :
32x32