Mehmet
Mehmet

Reputation: 3347

Swift: MapKit Custom Image Annotation

In my app, I have a mapkit and annotations with different colors. But, there are just three color options (red, green, purple). So, I need to change annotations with custom image.

I've followed this tutorial to create my mapview

Now, I have a Artwork class:

import Foundation
import MapKit

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

    init(title: String, locationName: String, color: String, coordinate: CLLocationCoordinate2D) {
        self.title = title
        self.locationName = locationName
        self.color = color
        self.coordinate = coordinate

        super.init()
    }

    var subtitle: String {
        return locationName
    }

    // pinColor for disciplines: Sculpture, Plaque, Mural, Monument, other
    func pinColor() -> MKPinAnnotationColor  {
        switch color {
        case "Red":
            return .Red
        case "Purple":
            return .Purple
        case "Green":
            return .Green
        default:
            return .Green
        }
    }
}

Also, VCMapView.swift file:

import Foundation
import MapKit

extension MapViewController: MKMapViewDelegate {

    // 1
    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
        if let annotation = annotation as? Artwork {
            let identifier = "pin"
            var view: MKPinAnnotationView
            if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
                as? MKPinAnnotationView { // 2
                    dequeuedView.annotation = annotation
                    view = dequeuedView
            } else {
                // 3
                view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
                view.canShowCallout = true
                view.calloutOffset = CGPoint(x: -5, y: 5)
                view.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as! UIView
            }
            view.pinColor = annotation.pinColor()
            return view
        }
        return nil
    }
}

And I can add pins on my map like this in viewdidload()

// show artwork on map
let artwork = Artwork(title: "\(self.plate)",
locationName: "\(self.location)",
color: "\(self.color)",
coordinate: CLLocationCoordinate2D(latitude: latitude, longitude: longtitude))

self.mapView.addAnnotation(artwork)

All work perfect but need to add custom image to this architecture, which part should I modify I am not sure.

Upvotes: 4

Views: 4688

Answers (1)

Nerkyator
Nerkyator

Reputation: 3976

In your architecture in my opinion, you should add an image property in the Artwork class and then use id in the .image property of MKPinAnnotationView.

ex:

view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
view.canShowCallout = true
view.calloutOffset = CGPoint(x: -5, y: 5)
view.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as! UIView
            }
view.image = UIImage(named:"your_image")
view.frame.size = CGSize(width: 30.0, height: 30.0) //not necessary but just to give you an idea how to resize just in case it is too large.

Upvotes: 5

Related Questions