カスタム注釈を選択すると、マップ上にカスタム注釈ビューが表示されます。アクションを作成して別のビューに注釈データを渡したいと思います。それが動作するための最良の方法は、組み込み関数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")
}
私はカスタムアノテーションを使用しないときはいつでも動作しますが、カスタムアノテーションを使用すると機能しません。私はカスタム注釈にターゲットを追加できますが、注釈データを使用する必要があるので、addTargetはうまくいかないでしょう。 – pprevalon
@Robそれを解決するためのコードを追加しました – pprevalon
@Robアノテーションにタイトルを追加することで 'didSelect'機能が有効になりました。私は 'placeRquestByUsersOnMap'から余分な注釈を削除します。ありがとうございました。あなたが回答を提出すれば、私はそれを受け入れてポイントポイントを得ることができます。 – pprevalon