代码之家  ›  专栏  ›  技术社区  ›  John

禁用MapKit Swift中的Mylocation Marker Tap

  •  1
  • John  · 技术社区  · 8 年前

    我正在苹果地图中添加MKAnnotationView,并在annotationView中创建一个tap事件。但Mylocation标记正在与annotationView点击交互。如何克服这个问题。希望你能理解我的问题。提前谢谢。

     func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    
        var annotationView = MKAnnotationView()
    
        guard let annotation = annotation as? ClientLocation
            else{
                return nil
        }
        if let dequedView = mapView.dequeueReusableAnnotationView(withIdentifier: annotation.identifier){
            annotationView = dequedView
        }
        else{
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotation.identifier)
        }
        ////////////////////////
        ///////////////////
        annotationView.rightCalloutAccessoryView = button
        annotationView.canShowCallout = true
        return annotationView
    }
    
    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    }
    

    enter image description here

    3 回复  |  直到 8 年前
        1
  •  5
  •   Carlos De la Mora    7 年前

    在您的代理人身上

     public func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
            let userView = mapView.view(for: mapView.userLocation)
            userView?.isUserInteractionEnabled = false
            userView?.isEnabled = false
            userView?.canShowCallout = false
    }
    
        2
  •  2
  •   Nisarg Atul Waghmare    7 年前

    这对我有用。试试这个

    override func viewDidLoad() {
        super.viewDidLoad()
        mapView.userLocation.title = nil
    }
    
        3
  •  1
  •   GIJOW    8 年前

    可以使用委托方法自定义注释视图 viewFor annotation 从…起 MKMapViewDelegate

    编辑:

    我只是想复制你的代码,让我怀疑的只是你的课程 ClientLocation . 如果您的 guard let return nil您必须检查此语句。

    您需要做的是检查视图类的类型是否来自您的委托,如下所示:

    if annotation.isKind(of: MKUserLocation.self) {
        // this means we are handling the blue point or the user location.
        //then you can set the parameters for this view like
        annotationView.canShowCallout = false
    
        // or even unable user to interact with
        annotationView.isUserInteractionEnabled = false
    }
    

    您的代理人应该是:

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{
        var annotationView = MKAnnotationView()
        guard let annotation = annotation as? ClientLocation
            else{
                return nil
        }
        if let dequedView = mapView.dequeueReusableAnnotationView(withIdentifier: annotation.identifier){
            annotationView = dequedView
        }
        else{
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotation.identifier)
        }
    
        if annotation.isKind(of: MKUserLocation.self) {
            annotationView.isUserInteractionEnabled = false
        }
        ////////////////////////
        ///////////////////
        annotationView.rightCalloutAccessoryView = button
        annotationView.canShowCallout = true
        return annotationView
    }