2017-01-02 27 views
1

カスタム注釈を選択すると、マップ上にカスタム注釈ビューが表示されます。アクションを作成して別のビューに注釈データを渡したいと思います。それが動作するための最良の方法は、組み込み関数func didSelectを使用する必要があります。私がカスタム注釈を選択するとき、何も起こらない。私がなぜdidSelect機能が呼び出されなかったのかを示していないのは、あらかじめありがとうございます。スウィフトカスタム注釈が選択されない

class RquestCustomPointAnnotation:MKPointAnnotation { 
    var image: String! 
} 

func placeRquestByUsersOnMap(){ 
     //retrieve item from firebase 
     var markerArray = [MKPointAnnotation]() 
     let path = "rquest/frontEnd/posts/userCreatedPost" 

     self.childRef(path).observe(.childAdded, andPreviousSiblingKeyWith: {snapshot, _ in 
      //get status 
      if let status = snapshot.childSnapshot(forPath: "status").value as? String { 
       if status == "pending"{ 
        let indentifier = "rquest" 
        if let coordinate = snapshot.childSnapshot(forPath: "coordinate").value as? NSDictionary { 
         let lat = coordinate["lat"] as! Double 
         let long = coordinate["long"] as! Double 
         let location = CLLocationCoordinate2D(latitude: lat, longitude: long) 
         let point = RquestCustomPointAnnotation() 
         let rquestView = MKAnnotationView(annotation: point, reuseIdentifier: indentifier) 
         point.image = "22" 

         let key = snapshot.key 
         point.coordinate = location 
         point.accessibilityValue = key 
         point.accessibilityLabel = "Rquest" 
         markerArray.append(point) 

         self.mapView.addAnnotation(rquestView.annotation!) 

         //create an obserarver to check if it is 
         let paths = "rquest/frontEnd/posts/userCreatedPost/\(key)/" 
         self.childRef(paths).observe(.childChanged, andPreviousSiblingKeyWith: { (snap, _) in 
          if snap.key == "status" { 
           if let status = snap.value as? String { 
            if status != "pending" { 
             for i in markerArray { 
              if i.accessibilityValue == key { 
               self.mapView.removeAnnotation(i) 
              } 
             } 
            } 
           } 
          } 
         }) 
        } 
       } 
      } 
     }) 
    } 

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
if annotation is RquestCustomPointAnnotation { 
      let reuseIdentifier = "rquest" 
      var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) 

      if annotationView == nil { 
       annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier) 
       annotationView?.canShowCallout = true 
      } else { 
       annotationView?.annotation = annotation 
      } 

      let customPointAnnotation = annotation as! RquestCustomPointAnnotation 
      annotationView?.image = UIImage(named: customPointAnnotation.image) 


      return annotationView 
     } 

} 


func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { 

    print("did select") 

    } 
+0

私はカスタムアノテーションを使用しないときはいつでも動作しますが、カスタムアノテーションを使用すると機能しません。私はカスタム注釈にターゲットを追加できますが、注釈データを使用する必要があるので、addTargetはうまくいかないでしょう。 – pprevalon

+0

@Robそれを解決するためのコードを追加しました – pprevalon

+0

@Robアノテーションにタイトルを追加することで 'didSelect'機能が有効になりました。私は 'placeRquestByUsersOnMap'から余分な注釈を削除します。ありがとうございました。あなたが回答を提出すれば、私はそれを受け入れてポイントポイントを得ることができます。 – pprevalon

答えて

1

あなたはtruecanShowCalloutセットを持っています。だから、それをタップすると、コールアウトが欲しいのですか、それともdidSelectに電話をかけますか? (通常、あなたはどちらか一方をしますが、両方は行いません)あなたの吹き出しを見ていますか?

私がもし(a)の注釈がtitleを持っていない、と(b)は注釈ビューのcanShowCallouttrueあり、その後、だけでなく、それは吹き出しを表示することはできませんが、それはまたdidSelectを防ぐという好奇心行動に気づきます呼び出されることから。

canShowCalloutfalseに設定するか、注釈にtitleが含まれていることを確認することができます。

関連する問題