我正在创建一个包含自定义注释引脚以及注释标注中的图像的地图。唯一的问题是我无法想出一种方法来为不同的注释提供不同的图像。更具体地说,我正在为注释标注提供单独的图像。虽然我已经找到了如何更改引脚的结果,但我正在努力寻找标注的任何信息,并希望得到任何帮助。以下是我正在使用的注释的功能。谢谢。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !annotation.isKind(of: MKUserLocation.self) else {
return nil}
let annotationIdentifier = "AnnotationIdentifier"
var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
annotationView = dequeuedAnnotationView
annotationView?.annotation = annotation
} else {
let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
av.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
annotationView = av
}
if let annotationView = annotationView {
// Configure your annotation view here
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "pinAnnotation.png")
}
let imageName = "until.png"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)
annotationView?.detailCalloutAccessoryView = imageView
return annotationView
}
class ViewController2: UIViewController {
@IBOutlet var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
//Location for first Pin
let locationOne = CLLocationCoordinate2DMake(-47.016945, 167.852095)
//Location for map centering
let locationNZ = CLLocationCoordinate2DMake(-43.937462, 170.507813)
let span = MKCoordinateSpanMake(9, 9)
let region = MKCoordinateRegion(center: locationNZ, span: span)
mapView.setRegion(region, animated: true)
//Create annotation one
let annotation = MKPointAnnotation()
annotation.coordinate = locationOne
annotation.subtitle = "Park"
annotation.title = "Rakiura National Park"
//Add annotation to the map
mapView.addAnnotation(annotation)
mapView.delegate = self}