Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change MKAnnotation Color using Swift?

I have MKAnnotations set up on a map, but I would like to change the color of the annotations for different scenario's. Is there a way to change the color of the annotation?

Here is my code below, how would I implement the color change?

override func viewDidAppear(animated: Bool) {
    var annotationQuery = PFQuery(className: "Post")
    currentLoc = PFGeoPoint(location: MapViewLocationManager.location)
    //annotationQuery.whereKey("Location", nearGeoPoint: currentLoc, withinMiles: 10)
    annotationQuery.whereKeyExists("Location")
    annotationQuery.findObjectsInBackgroundWithBlock {
        (points, error) -> Void in
        if error == nil {
            // The find succeeded.
            println("Successful query for annotations")
            // Do something with the found objects

            let myPosts = points as! [PFObject]

            for post in myPosts {
                let point = post["Location"] as! PFGeoPoint
                let annotation = MKPointAnnotation()
                annotation.coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude)
                annotation.title = post["title"] as! String!
                annotation.subtitle = post["username"] as! String!


                self.mapView.addAnnotation(annotation)
            }

        } else {
            // Log details of the failure
            println("Error: \(error)")
        }
    }
like image 558
Zach Avatar asked Apr 24 '15 08:04

Zach


2 Answers

You can use custom images for annotation view or use predefined MKPinAnnotationView with pinColor. But pinColors limited to Red, Green and Purple.

Some example:

import UIKit
import MapKit

class Annotation: NSObject, MKAnnotation
{
    var coordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
    var custom_image: Bool = true
    var color: MKPinAnnotationColor = MKPinAnnotationColor.Purple
}

class ViewController: UIViewController, MKMapViewDelegate {

@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.mapView.delegate = self;

    let annotation = Annotation.new()
    mapView.addAnnotation(annotation)

    let annotation2 = Annotation.new()
    annotation2.coordinate = CLLocationCoordinate2D(latitude: 0.0, longitude: 1.0)
    annotation2.custom_image = false
    mapView.addAnnotation(annotation2)

    let annotation3 = Annotation.new()
    annotation3.coordinate = CLLocationCoordinate2D(latitude: 1.0, longitude:  0.0)
    annotation3.custom_image = false
    annotation3.color = MKPinAnnotationColor.Green
    mapView.addAnnotation(annotation3)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if (annotation is MKUserLocation) {
        return nil
    }

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        if let anAnnotation = annotation as? Annotation {
            if anAnnotation.custom_image {
                let reuseId = "custom_image"
                anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
                anView.image = UIImage(named:"custom_image")
            }
            else {
                let reuseId = "pin"
                let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
                pinView.pinColor = anAnnotation.color
                anView = pinView
            }
        }
        anView.canShowCallout = false
    }
    else {
        anView.annotation = annotation
    }

    return anView
}
}

Update: Set delegate for mapView in viewDidLoad

like image 70
Oleg Shulakov Avatar answered Oct 22 '22 13:10

Oleg Shulakov


You have to set it in the viewForAnnotation method. In this tutorial is quite well explained how doing it.

like image 42
Nerkyator Avatar answered Oct 22 '22 14:10

Nerkyator