0

I did everything on the answer of this link: Swift different images for Annotation

But the marker icon won't change it just stays the same I tried every way I could find on google but they all give the same result.

This is my code:

import UIKit
import MapKit

class Kaart: UIViewController, MKMapViewDelegate{
    @IBOutlet weak var mapView: MKMapView!
    let initialLocation = CLLocation(latitude: 52.20614380, longitude: 21.04759094)
    let markerLocation = CLLocationCoordinate2D(latitude: 52.20614380, longitude: 21.04759094)

    override func viewDidLoad() {
        super.viewDidLoad()
        UINavigationBar.appearance().translucent = false;
        UINavigationBar.appearance().barTintColor = UIColor(red: CGFloat(193.0/255.0), green: 58/255, blue: 44/255, alpha: 1)
        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]


        //Set Default location
        centerMapOnLocation(initialLocation)

        //Place markers
        var info1 = CustomPointAnnotation()
        info1.coordinate = CLLocationCoordinate2DMake(42, -84)
        info1.title = "Info1"
        info1.subtitle = "Subtitle"
        info1.imageName = "vlag.png"
        mapView.addAnnotation(info1)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    let regionRadius: CLLocationDistance = 1000
    func centerMapOnLocation(location: CLLocation) {
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
            regionRadius * 50.0, regionRadius * 50.0)
        mapView.setRegion(coordinateRegion, animated: true)
    }

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

        let reuseId = "test"

        var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
        if anView == nil {
            anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            anView.canShowCallout = true
        }
        else {
            anView.annotation = annotation
        }

        //Set annotation-specific properties **AFTER**
        //the view is dequeued or created...

        let cpa = annotation as! CustomPointAnnotation
        anView.image = UIImage(named:cpa.imageName)

        return anView
    }

}

class CustomPointAnnotation: MKPointAnnotation {
   var imageName: String!
}
Community
  • 1
  • 1
Sinan Samet
  • 6,432
  • 12
  • 50
  • 93
  • 1
    It seems you aren't setting the delegate of your `mapView`..try to add `mapView.delegate = self` in your `viewDidLoad` – Nerkyator Aug 03 '15 at 10:31
  • Wow thank you that worked I knew it should've been a small thing but I just couldn't figure it out thanks a lot – Sinan Samet Aug 03 '15 at 10:34

0 Answers0