如何在iOS 14中去除MKBalloonCalloutView

3

我有以下代码,适用于 iOS 13 及以下版本。

func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
    mapView.userLocation.title = "You are here"
    mapView.userLocation.subtitle = // user's location
}

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

    if annotation.isKind(of: MKUserLocation.self) {
        return nil
    }
}

它只显示蓝点,没有标注,蓝点上方只有标题和副标题。

enter image description here

iOS 14中,出现了默认的MKBalloonCalloutView来代替标题和副标题。它显示了一个灰色的profileImage。我该如何摆脱BalloonCallout,以便只显示标题和副标题?

enter image description here

enter image description here

2个回答

0

对于用户(问题中的那个),如果使用 iOS 14 及更高版本,我使用 MKMarkerAnnotationView。如果使用 iOS 13 或更低版本,则使用 MKPinAnnotationView。我为其他人单独设计了自定义图钉:

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

    let reuseIdentifier = "MyIdentifier"

    if annotation.isKind(of: MKUserLocation.self) {

        if #available(iOS 14.0, *) {
            if let pin = mapView.dequeueReusableAnnotationView(withIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier) as? MKMarkerAnnotationView {
                
                return setMKMarkerAnnotationView(pin: pin)
            }
        } else {
            let pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
            pin.canShowCallout = true
            return pin
        }
        return nil

    } else {

        // dequeue the custom pins for everyone else
    }
}

func setMKMarkerAnnotationView(pin: MKMarkerAnnotationView) -> MKMarkerAnnotationView {
    
    pin.animatesWhenAdded = true
    
    pin.markerTintColor = UIColor.red
    pin.titleVisibility = .visible
    pin.subtitleVisibility = .visible
    
    return pin
}

0
通过为用户位置标注的MKAnnotationView设置自己的详细detailCalloutAccessoryView,行为将恢复到仅显示标题和副标题。
您可以设置任何您选择的UIView,例如UIImageView,或者只是一个空的UIView。
例如,在您的MKMapViewDelegate中。
func mapViewDidFinishLoadingMap(_ mapView: MKMapView) {
    mapView.view(for: mapView.userLocation)?.detailCalloutAccessoryView = .init()
}

抱歉回复晚了。我正在处理另一个项目,没有时间深入研究使用MKMapView的项目。无论如何,你的答案部分有效。问题是“title”显示出来了,但“subtitle”没有出现。你有什么想法,可能是什么问题呢? - Lance Samaria

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接