2017-11-09 23 views
1

저는 현재 firebase에서 데이터를 가져 와서 내 MKMapKitView에 주석을 작성하려고합니다. 데이터를 올바르게 검색하지만 주석을 제대로 작성하지 않는다고 생각합니다.Firebase에서 좌표를 가져 와서 주석을 작성하십시오.

나는 여러 사용자가 있기 때문에 주석 달기의 일반적인 방법으로 설정할 수는 없다고 생각합니다.

let userLocation = CLLocationCoordinate2D(latitude: Double(userLatitude!), longitude: Double(userLongitude!)) 

       let userAnnotation = MKPointAnnotation(); 
     userAnnotation.coordinate = self.userLocation!; 
       //userAnnotation.title = "Riders Location"; 
       map.addAnnotation(userAnnotation); 

     } 

또한 사용자를 검색하는 방법도 추가 할 것입니다.

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 userLongitude = value["long"] as? Double, 
          let userLatitude = value["lat"] as? Double 


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

          self.user.append(userToShow) 
         } 


        } 

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

       //not sure if this is right 
      } 
     }) 

고맙습니다!

+0

안녕하세요 Becca, 오류 메시지가 나타 났습니까? 지도에서 실제로 무엇을 볼 수 있습니까? 당신이 생각하는 것을 이해하기위한 다른 세부 정보는 잘못 되었나요? –

답변

0

그것은 직감이지만, 당신이이 같은 기능을 수행했다고 생각합니다. - 당신은 꽤 가까이에 노력했습니다! NB - 신속한 구문에서 세미콜론이 없습니다.

private func addUserAnnotation(user: User) {   

    let userAnnotation = MKPointAnnotation() 
    let userLocation = CLLocationCoordinate2D(latitude: Double(user.userLatitude!), 
longitude: Double(user.userLongitude!)) 

    userAnnotation.coordinate = userLocation 
    //userAnnotation.title = "Riders Location" 
    self.map.addAnnotation(userAnnotation) 

} 

콜과 같이 기능 -의 당신이 당신의 사용자 배열에서 단지 첫 번째 사용자에 대한 주석을 추가 할 가정 해 봅시다 : 여기

addUserAnnotation(user: user[0]) //addUserAnnotation(user[0]) also acceptable 

는 사용자의 OP의 클래스입니다. 이 또한 중요하다고 생각합니다.

class User: NSObject { 

    var userID: String! 
    var fullName: String! 
    var imagePath: String! 
    var userLongitude: Double! // change from String! 
    var userLatitude: Double! // change from String! 

} 
+0

"MKPointAnnotation이 아닌 함수 타입의 값을 호출 할 수 없습니다"라는 오류 메시지가 나타납니다. Firebase는 모든 데이터를 문자열로 저장합니다 ... 도와 주셔서 감사합니다! –

+0

걱정하지 마시고, 테스트를 실행하여 사용자 속성 userLatitude 및 userLongitude에서 double 형변환 문제가 있는지 확인해 봅시다. 어딘가에서 십진수를 추가하여지도에 주석이 추가되는지 확인하는 방법은 어떨까요? addUserAnnotation 함수의 맨 위에서 print (user.userLatitude) 및 print (user.userLongitude)를 수행하여 적절한 좌표를 찾을 수 있습니다. –

+0

사용자 좌표를 인쇄 할 때 아무 것도 표시되지 않습니다. 좌표를 연결하고 작동하게 할 수 있습니까? 나는 거기에서 시작할 것입니다. –