0

I am trying to create an app that uses the MapKit and I want to create annotations on the map, but I am struggling to put images on the map annotations.

I am a beginner with Swift programming, I have been working off this tutorial, but I can't find anywhere online that helps you implement an image, most guides are using Objective C.

This is my annotation class, which is called in my View Controller.

Note: I have my little image I want to be shown in my images.xcassets folder as "mapAnnotation"

let buildingName = location(title: "Building Name",
                            locationName: "Location",
                            coordinate: CLLocationCoordinate2D(latitude: NUMBER, longitude: NUMBER))

theMapview.addAnnotation(buildingName)

Here is the annotation class...

class location: NSObject, MKAnnotation {
    let title: String
    let locationName: String
    let coordinate: CLLocationCoordinate2D

    //Retrieve an image
    var image = UIImage(named: "mapAnnotation")

    init(title: String, locationName:String, coordinate: CLLocationCoordinate2D) {
        self.title = title
        self.locationName = locationName
        self.coordinate = coordinate
        self.image
        super.init()
    }

    var subtitle: String {
        return locationName
    }
}
Brian
  • 14,610
  • 7
  • 35
  • 43
  • See http://stackoverflow.com/questions/25631410/swift-different-images-for-annotation, http://stackoverflow.com/questions/24467408/swift-add-mkannotationview-to-mkmapview. –  May 12 '15 at 15:45

1 Answers1

0

You're on the right track.

An MKAnnotation is an abstract object that stores information about a map annotation.

Now you need to create a custom subclass of MKAnnotationView. That is an actual view object that gets added to the map view at the annotation's location. It can be as simple as a subclass of UIImageView.

Then you'll need to implement the MKMapViewDelegate method mapView:viewForAnnotation:. That method takes an annotation as input, and returns an MKAnnotationView.

See the Xcode docs on MKAnnoationView and the mapView:viewForAnnotation: method for more information.

Duncan C
  • 128,072
  • 22
  • 173
  • 272