2017-11-09 18 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

こんにちはベッカ、何かエラーメッセージが表示されますか?あなたのマップには実際に何が表示されますか?私たちがあなたの考えを間違っていると感じる感情を得るためのその他の詳細は? –

答えて

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型にキャストするのに問題がないかどうかを確認しましょう。いくつかの10進数をそこに追加し、注釈がマップに追加されているかどうかを確認するだけです。 addUserAnnotation関数の上部にあるprint(user.userLatitude)とprint(user.userLongitude)を実行することで、適切な座標を見つけることができます。 –

+0

ユーザーの座標を印刷しようとすると何も表示されません。座標を差し込んで作業させることもできません...私はそこから始めます。 –

関連する問題