ios - 如何防止自动快速取消选择注释

标签 ios swift mapkit mkmapviewdelegate

我有一个 View ,里面有一张 map 。我想实现以下目标:当用户点击 map 时显示并选择注释(因此显示标注气泡)直到用户点击 map 上的另一个点(在这种情况下,应该删除之前的注释).

问题是,当我点击 map 时,注释会被添加并选中一段时间​​,然后不知何故它被取消选中(因此标注消失)。我希望标注气泡持续存在,直到用户点击另一个点

下面是我的尝试,请问有没有人能帮帮我?

@objc func handleTap(_ sender: UITapGestureRecognizer){

    //...some code that computes the country code starting from latitude and longitude

            var countryCurrency = ""

            func countryRequest(countryLabel: String, completion: @escaping (String)->()){
                //api request that receives the currency name of the country
                completion(countryCurrency)
            }
            countryRequest(countryLabel: countryLabel){ countryCurrency in

                DispatchQueue.main.async {

                    let locat:CLLocationCoordinate2D = CLLocationCoordinate2DMake(locationCoord.latitude, locationCoord.longitude)
                    let annotation = MKPointAnnotation()
                    annotation.coordinate = locat
                    annotation.title = countryCurrency
                    annotation.subtitle = "subtitle"
                    self.mapView.addAnnotation(annotation)
                    self.mapView.selectAnnotation(annotation, animated: true)

                }

            }
}

我还有一个 MKMapViewDelegate 的扩展来自定义标注:

extension MapsItem: MKMapViewDelegate {

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    if !(annotation is MKPointAnnotation){
        return nil
    }

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "demo")
    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "demo")
        annotationView!.canShowCallout = true
        annotationView!.sizeToFit()
    }
    else{
        annotationView!.annotation = annotation
    }

    return annotationView

}
}

提前致谢!!

最佳答案

试试这个

在添加注释之前将 isTappingNow 设置为 true,在选择它之后设置为 false

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

      if(!isTappingNow)
      {

           self.mapView.selectAnnotation(view.annotation, animated: false)
      } 

    }

关于ios - 如何防止自动快速取消选择注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48033517/

相关文章:

ios - 放大用户位置 - Swift

ios - 滑动手势 View Controller Xcode 5.1.1

swift - 理解 try catch , swift3 xcode

ios - 无法在 Swift 中遵循 MKAnnotation 协议(protocol)

ios - 尽管在 Swift 中使用 prepareToPlay(),AVAudioPlayer 仍会产生滞后

ios - LazyVStack 和 SwiftUI 的性能问题

ios - MKOverlayPathView 需要 drawMapRect 吗?

ios - 计算属性生成 "Overriding declaration requires an ' 重写“关键字”错误

ios - Facebook iOS SDK 4.2 : Invalid URL Schemes for Multiple App Environments

ios - Flutter软件包构建错误“未找到swift文件”