无法在MapKit中更改标记图标

时间:2015-08-03 09:54:48

标签: xcode swift

我在这个链接的答案上做了一切: Swift different images for Annotation

但是标记图标不会改变它只是保持不变我尝试了我在谷歌上找到的各种方式,但它们都给出了相同的结果。

这是我的代码:

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!
}

0 个答案:

没有答案
相关问题