【问题标题】:Custom MKPointAnnotation isn't responding to user interaction自定义 MKPointAnnotation 没有响应用户交互
【发布时间】:2017-05-20 16:15:06
【问题描述】:

我正在制作一个使用 MKPointAnnotations 的 Swift 应用程序,我最近遇到了一个问题,我需要在注释中存储元数据,因此我创建了下面的自定义类:

class BRETTFAnnotation: MKPointAnnotation {

    var tag: Int64

    var name: String

    init(lat : Double, lon:Double, t : Int64, n: String) {

        self.tag = t
        self.name = n

        super.init()


        self.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
    }

}

我的MKAnnotationViewviewforMKAnnotation方法如下图:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let newAnnotation = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "reuse")

    newAnnotation.canShowCallout = true

    let right = self.button(title: "Yes")
    right?.addTarget(self, action: #selector(clickedToConfirmNewPoint), for: .touchUpInside)
    newAnnotation.rightCalloutAccessoryView = right

    let left = self.button(title: "No")
    left?.addTarget(self, action: #selector(clickedToCancelNewPoint), for: .touchUpInside)
    newAnnotation.leftCalloutAccessoryView = left

    return newAnnotation
}

我遇到的问题是,当我点击我的自定义BRETTFAnnotation(我添加到我的MKMapView)时,什么也没有发生。当我只使用MKPointAnnotation(而不是BRETTFAnnotation)时,当我单击地图时,MKAnnotationView 上的两个按钮会显示。 我正在尝试使用我的BRETTFAnnotation 而不是MKPointAnnotationMKPinAnnotationView 在触摸时显示。

当用户同时点击注释时,如何继续使用我的自定义注释并显示标注?

编辑 1:因为它可能有用,所以下面的代码是我如何制作注释并将其添加到 mapView。

        let location = gestureRecognizer.location(in: mapView)
        let coordinate = mapView.convert(location,toCoordinateFrom: mapView)

        print("adding lat,long \(coordinate.latitude),\(coordinate.longitude)")


        lastPoint = BRETTFAnnotation(lat: coordinate.latitude, lon: coordinate.longitude, t: 1, n: "")

        let annotationView = MKPinAnnotationView(annotation: lastPoint, reuseIdentifier: "reuse")

        mapView.addAnnotation(lastPoint)

【问题讨论】:

  • 你到底想做什么?使用 mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) 来处理注解的点击。
  • @kuzdu 我以前没有使用过,但是现在当我点击我的自定义注释时,标注没有显示。我只是尝试使用任何方法处理自定义注释的点击,如果您引用的方法可以工作,请解释如何。

标签: swift mapkit mkannotation mkpinannotationview


【解决方案1】:

当您使用自己的 MKAnnoation 时,您可以在 didSelect 中处理您的操作。只需执行以下代码即可。

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    if let yourAnnotation = view.annotation as? BRETTFAnnotation {
       //handle your meta data or/and show UIViews or whatever
    }
}

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
    //getting called when you tap on map or on another annotation (not the selected annotation before) 
    //hide UIViews or do whatever you want
 }

这对我有用:

class ViewController: UIViewController, MKMapViewDelegate {

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


    print("didSelect")
    if let annoation = view.annotation as? MyAnnoation {
        print("metatag \(annoation.metaTag)")
    }
}

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
    print("didDeselect")
}

override func viewDidLoad() {
    super.viewDidLoad()


    mapView.delegate = self
    let annotation = MyAnnoation(n: "name", m: "metaTag")
    annotation.coordinate = CLLocationCoordinate2D(latitude: 50.0, longitude: 8.0)
    mapView.addAnnotation(annotation)
   }
 }



class MyAnnoation: MKPointAnnotation {


var name: String?
var metaTag: String?


init(n: String, m: String) {
    self.name = n
    self.metaTag = m
  }
}

【讨论】:

  • 您的第一种方法似乎没有响应我对注释的点击(我添加了打印语句,点击时没有打印任何内容)。这是否意味着我只是在其他地方做错了什么,或者它仍然与我的自定义类有关?
  • 另外,我的自定义 BRETTFAnnotationMKPointAnnotation 的子类,而不是 MKAnnotationView 的子类(不过我不知道这是否会有所不同)。
  • 您是否设置了 mapview.delegate = self ?是 didSelect 没有响应还是 let 里面的语句?
  • 是的,我在 viewDidLoad() 中这样做了
  • 我编辑了我的问题以包括我是如何创建/添加我的注释的,这样你就可以在那里找到错误的地方。
【解决方案2】:

我通过将BRETTFAnnotation 设为NSObjectMKAnnotation 的子类而不是MKPointAnnotation 来解决此问题。这样做允许我的自定义类接收用户交互并显示标注。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 2021-04-01
    • 1970-01-01
    相关资源
    最近更新 更多