我正在尝试在我正在使用的mapkit上实现注释
mapView.register(EventMarkerView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
但是,我经常看到这种格式
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard let annotation = annotation as? Event else { return nil }
let identifier = "marker"
var view: MKMarkerAnnotationView
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
as? MKMarkerAnnotationView {
dequeuedView.annotation = annotation
view = dequeuedView
} else {
view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
view.canShowCallout = true
view.calloutOffset = CGPoint(x: -5, y: 5)
view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}
return view
}
如果注释类型不止一种,我会使用后一种形式吗?
我也很好奇我的MKMapViewDefaultAnnotationViewReuseIdentifier
是否有效?我没有选择它,但是由于我只有1种注释类型,没关系,对吗?
最后,重用标识符是否仍具有dequeueReusableAnnotationView(withIdentifier: identifier)
的功能?
答案 0 :(得分:0)
这里有很多事情要考虑:
MKMapViewDefaultAnnotationViewReuseIdentifier
仅在 iOS 11 + 上受支持,因此,如果要定位较早的iOS,则可能要使用后一种形式。底线:
如果仅定位到iOS 11+,并且只有1个注释,则可以使用MKMapViewDefaultAnnotationViewReuseIdentifier
。否则请考虑其他形式。
希望这可以帮助您做出决定。