2

我有以下适用于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
    }
}

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

在此处输入图像描述

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

在此处输入图像描述

在此处输入图像描述

4

2 回答 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
}
于 2022-01-29T05:32:41.357 回答
0

detailCalloutAccessoryView通过为用户位置注释设置您自己的详细信息,MKAnnotationView行为将恢复为仅显示标题和副标题。

你可以设置任何UIView你的选择,比如一个UIImageView例子,或者只是一个空的。

例如在你的MKMapViewDelegate

func mapViewDidFinishLoadingMap(_ mapView: MKMapView) {
    mapView.view(for: mapView.userLocation)?.detailCalloutAccessoryView = .init()
}
于 2022-01-11T11:20:04.100 回答