为什么默认的mapkit注释不会在iOS模拟器中的地图上不呈现

问题描述 投票:0回答:1

我正在使用MKAnnotation协议来帮助在我的iOS模拟器地图上显示默认标记注释,但看不到它呈现。

这是我创建的DriverAnnotation类;

class DriverAnnotation: NSObject, MKAnnotation {
    var coordinate: CLLocationCoordinate2D
    var uid: String

    init(uid: String, coordinate: CLLocationCoordinate2D) {
        self.uid = uid
        self.coordinate = coordinate
    }
}

这里是使用它的代码,应该在地图上显示标记注释;

    func fetchDrivers() {
        guard let location = locationManager?.location else { return }
        Service.shared.fetchDrivers(location: location) { (driver) in
            guard let coordinate = driver.location?.coordinate else { return }
            let annotation = DriverAnnotation(uid: driver.uid, coordinate: coordinate)
            self.mapView.addAnnotation(annotation)
        }

    }

那么为什么注释不会在iOS模拟器地图上呈现?我所得到的只是当前位置缓慢闪烁的蓝点。

map annotation missing

ios swift annotations mapkit
1个回答
0
投票

displayPriorityMKMarkerAnnotationView默认为.defaultLow(尽管the documentationMKAnnotationViewdisplayPriority建议应默认为.required

因此,您想要一个MKMarkerAnnotationView.required,然后要声明一个注释视图类,将该类设置为.required

class RequiredMarkerAnnotationView: MKMarkerAnnotationView {
    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        displayPriority = .required
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override var annotation: MKAnnotation? {
        didSet {
            displayPriority = .required
        }
    }
}

mapView.register(RequiredMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
© www.soinside.com 2019 - 2024. All rights reserved.