如果注释标注与另一个注释重叠,则无法点击?

时间:2019-05-24 15:59:22

标签: ios swift mapkit

我正在使用MKMapView和自定义MKMarkerAnnotationViewdisplayPriority = .required上显示一组注释,这样就不会出现聚类或隐藏,而将UIButton作为rightCalloutAccessoryView

当我点击地图上的注释时,标注将按预期显示,但是当我点击标注或它的附件时,如果该点击与地图上的另一个标记重叠,则该点击不会注册。

以下是该问题的游乐场友好示例。请注意,标注与地图上的另一条注释重叠时,标注如何不响应。

import MapKit
import PlaygroundSupport

class MapViewController: UIViewController, MKMapViewDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
                 calloutAccessoryControlTapped control: UIControl) {
        print("Callout tapped!")
    }
}

class CustomAnnotationView: MKMarkerAnnotationView {
    override var annotation: MKAnnotation? {
        willSet {
            canShowCallout = true
            rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
            titleVisibility = .hidden
            subtitleVisibility = .hidden
            displayPriority = .required
        }
    }
}

let mapView = MKMapView(frame: CGRect(x:0, y:0, width:800, height:800))
let controller = MapViewController()
mapView.delegate = controller

mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)

let coordinate1 = CLLocationCoordinate2DMake(37.334922, -122.009033)
let annotation1 = MKPointAnnotation()
annotation1.coordinate = coordinate1
annotation1.title = "Annotation 1"
annotation1.subtitle = "Subtitle 1"

let coordinate2 = CLLocationCoordinate2DMake(37.335821492347556, -122.0071341097355)
let annotation2 = MKPointAnnotation()
annotation2.coordinate = coordinate2
annotation2.title = "Annotation 2"
annotation2.subtitle = "Subtitle 2"

mapView.addAnnotation(annotation1)
mapView.addAnnotation(annotation2)

var mapRegion = MKCoordinateRegion()
let mapRegionSpan = 0.02
mapRegion.center = coordinate1
mapRegion.span.latitudeDelta = mapRegionSpan
mapRegion.span.longitudeDelta = mapRegionSpan
mapView.setRegion(mapRegion, animated: true)

let mapViewController = MapViewController()
PlaygroundPage.current.liveView = mapView

还有一个图像来说明问题。

Overlapping annotations

在此方面的任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

我的队友能够解决此问题。想法是在选择注释时禁用地图上所有其他注释的用户交互,然后在取消选择注释时重新启用它。

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    for nearbyAnnotation in mapView.annotations {
        let annotationView = mapView.view(for: nearbyAnnotation)
        if annotationView != nil {
            annotationView!.isUserInteractionEnabled = false
        }
    }
    view.isUserInteractionEnabled = true
}

func mapView(_ mapView: MKMapView, didDeselect _: MKAnnotationView) {
    for nearbyAnnotation in mapView.annotations {
        let annotationView = mapView.view(for: nearbyAnnotation)
        if annotationView != nil {
            annotationView!.isUserInteractionEnabled = true
        }
    }
}