在MapKit Swift中禁用Mylocation Marker Tap

时间:2018-03-19 18:34:12

标签: ios swift swift3 mapkit mkannotation

我在Apple地图中添加了MKAnnotationView,并在annotationView.But中创建了一个tap事件.Mylocation标记正在与annotationView tap进行交互。如何克服这个问题。希望你能理解我的问题。提前谢谢。

 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 个答案:

答案 0 :(得分:1)

您可以使用viewFor annotation中的委托方法MKMapViewDelegate自定义注释视图。

编辑:

我只是尝试重现您的代码,让我怀疑的只是您的班级ClientLocation。如果您的guard let返回无,则必须查看此声明。

您需要做的是检查来自您的代理的视图类的类型,如:

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
}

答案 1 :(得分:0)

在您的代表上

 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 :(得分:0)

它为我工作。试试这个

override func viewDidLoad() {
    super.viewDidLoad()
    mapView.userLocation.title = nil
}