我想自定义我的注释(图钉),以便能够更改其图像。我使用了一个函数(mapview)来改变图片,所以不知道是否应该再次使用它来改变另一组引脚的图片;假设我有两个地点,一个餐厅和一个医院,所以我想分别放一个盘子和一个红十字。这是我的功能代码:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "identifier")
if annotationView == nil{
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "identifier")
annotationView!.canShowCallout = true
annotationView!.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
let pinImg = UIImage(named: "curz")
let size = CGSize(width: 50, height: 50)
UIGraphicsBeginImageContext(size)
pinImg!.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
annotationView!.image = resizedImage
}
else {
annotationView!.annotation = annotation
}
return annotationView
}