0

I have created a map which contains a single annotation. From what I've seen, this is a very simple way of achieving this which I got off a tutorial. I am currently trying to get a custom picture as the annotation but am struggling to do so as almost all information on this topic is in objective C. Forgive me if it is really simple to do but I am relatively new to coding and would appreciate any help :)

class ViewController2: UIViewController {
@IBOutlet var Map: MKMapView!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

//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)
Map.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
Map.addAnnotation(annotation)}
Nick0014
  • 5
  • 1
  • 3

2 Answers2

0

I assume its a different image of the default pin you want? I've used this answer from another question, check it out here Custom pin image in annotationView in iOS

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
// Don't want to show a custom image if the annotation is the user's location.
guard !annotation.isKindOfClass(MKUserLocation) else {
    return nil
}

let annotationIdentifier = "AnnotationIdentifier"

var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(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: "yourImage")
}

return annotationView
}
Community
  • 1
  • 1
jonask
  • 679
  • 2
  • 6
  • 21
  • Sorry but I am not the best with Xcode and have a question about implementing the code in the answer. I have placed it at the end of the class right before the closing bracket however the function is never used. Is it in the wrong position or do I have to call it? Again sorry for my ignorance. – Nick0014 Jan 05 '17 at 21:49
  • Hi Nick. Could you try to edit your question, with how the codes look now? – jonask Jan 06 '17 at 09:20
0

updating @Jonask's answer

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        // Don't want to show a custom image if the annotation is the user's location.
        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: "yourImage")
        }

        return annotationView
}
James Palfrey
  • 753
  • 6
  • 29