MapView viewForAnnotation 在圆圈中显示白色图钉(默认)
MapView viewForAnnotation show white pin in circle (default)
当我没有在 MKMapView
上设置代表但添加注释时,他们的图像如下所示:
但是如果我 return 一个 MKAnnotationView
像这样:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation.isKind(of: MKUserLocation.self) {
return nil
}
guard annotation is MKPointAnnotation else { return nil }
let identifier = "Annotation"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.collisionMode = .circle
annotationView!.canShowCallout = true
} else {
annotationView!.annotation = annotation
}
return annotationView
}
然后图标看起来像一个垂直图钉:
我知道我可以在 MKAnnotationView
上使用自定义图像。但是,我想要默认的,除了我希望它看起来像第一个而不是第二个,但我也想自定义标注。我怎样才能做到这一点?
第一张图片属于 MKMarkerAnnotationView。所以要求它,而不是 MKPinAnnotationView。
当我没有在 MKMapView
上设置代表但添加注释时,他们的图像如下所示:
但是如果我 return 一个 MKAnnotationView
像这样:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation.isKind(of: MKUserLocation.self) {
return nil
}
guard annotation is MKPointAnnotation else { return nil }
let identifier = "Annotation"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.collisionMode = .circle
annotationView!.canShowCallout = true
} else {
annotationView!.annotation = annotation
}
return annotationView
}
然后图标看起来像一个垂直图钉:
我知道我可以在 MKAnnotationView
上使用自定义图像。但是,我想要默认的,除了我希望它看起来像第一个而不是第二个,但我也想自定义标注。我怎样才能做到这一点?
第一张图片属于 MKMarkerAnnotationView。所以要求它,而不是 MKPinAnnotationView。