我正在尝试给我的两个MKPointAnnotation引脚一个自定义图像。到目前为止,我能够在两个引脚上显示相同的图像。我可以对下面的代码做些什么来给每个引脚一个自定义的图像?
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !(annotation is MKUserLocation) else {
return nil
}
let annotationIdentifier = "AnnotationIdentifier"
var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
}
else {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}
if let annotationView = annotationView {
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "Moto_pin")
}
return annotationView
}发布于 2016-09-23 05:08:20
您需要更改每个引脚的图像,而不是annotationView.image = UIImage(named: "Moto_pin")。该行将为每个注释视图分配相同的图像。
发布于 2016-09-23 05:31:46
尝试使用MKPinAnnotationView而不是MKAnnotationView。要做到这一点,你只需要改变:
MKPinAnnotationView annotationView: var?
使用这个类,你可以自定义大头针的颜色和整个视图
https://stackoverflow.com/questions/39648864
复制相似问题