ios - 为什么iOS模拟器中默认的mapkit注解没有渲染到地图上
问题描述
我正在使用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 模拟器地图上渲染呢?我得到的只是当前位置缓慢闪烁的蓝点。
解决方案
displayPriority
ofMKMarkerAnnotationView
默认为(.defaultLow
尽管's的文档建议它应该默认为)。MKAnnotationView
displayPriority
.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)
推荐阅读
- c# - 从字符声明字符串会产生意外结果。为什么会这样?
- javascript - 克隆猫鼬文档属性
- terraform - 如何将 azure_function_endpoint 添加到事件网格订阅?
- python - Django:ROOT_URLCONF:如何用两个“urls.py”区分两个同名文件夹?
- java - 请求以错误的编码发送,或者我错误地填写了请求
- excel - Excel CountIfs 公式过滤日期
- android - 是否可以在没有动画的情况下在 MotionLayout 中设置结束状态?
- powershell - 根据当前时间更新多个条目的 Powershell 脚本
- ruby-on-rails - 有没有办法在没有数据库连接的情况下在 Rails 中启动 Rails puma 网络服务器?
- python - 你如何获得一个正则表达式来匹配一个案例中的一个案例?