Swift 3.0 Pin颜色注释在MapView中不会更改

时间:2017-05-18 01:42:34

标签: swift annotations mkmapview

我有兴趣根据案例场景更改注释的引脚颜色。在一个函数中,我发送了一个数组,用于确定引脚注释的颜色。到目前为止,我已经设置了一个名为ColorPointAnnotation的子类,它将确定pinColor。然后,在switch语句中,我为注释设置ColorPointAnnotation。在viewForAnnotation中,我将新注释与颜色一起放置。根据我对文档注释的理解,这就是所需要的,但是,mapView上的引脚颜色没有变化。我不确定可能是什么问题。

class ColorPointAnnotation: MKPointAnnotation {
    var pinColor: UIColor

    init(pinColor: UIColor) {
        self.pinColor = pinColor
        super.init()
    }
}

//here is the function that pulls the array from another view controller
func add(newLocation location_one:[String:Any]) {

    let momentaryLat = (location_one["latitude"] as! NSString).doubleValue
    let momentaryLong = (location_one["longitude"] as! NSString).doubleValue

    var annotation = MKPointAnnotation()

    switch String(describing: location_one["eventType"]!) {
        case "No":
            print("Attending Event")
            annotation = ColorPointAnnotation(pinColor: UIColor.red)
        case "Yes":
            print("Hosting Event")
            annotation = ColorPointAnnotation(pinColor: UIColor.green)
        default:
            print("The pin color is purple")
            annotation = ColorPointAnnotation(pinColor: UIColor.purple)
    }

    annotation.title = location_one["title"] as? String
    annotation.coordinate = CLLocationCoordinate2D(latitude: momentaryLat as CLLocationDegrees, longitude: momentaryLong as CLLocationDegrees)


    DispatchQueue.main.async {
        self.map.addAnnotation(annotation)
    }

    self.map.centerCoordinate = annotation.coordinate

}

func mapView(_ map: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    //        if (annotation is MKUserLocation) {
    //            return nil
    //        }

    let identifier = "pinAnnotation"
    var annotationView = map.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView


    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView?.canShowCallout = true
        let colorPointAnnotation = annotation as! ColorPointAnnotation
        annotationView?.pinTintColor = colorPointAnnotation.pinColor


    }
    //      else {
    //            annotationView?.annotation = annotation
    //
    //        }
    //        map.showAnnotations(map.annotations, animated: true)
    return annotationView
}

1 个答案:

答案 0 :(得分:1)

我认为您的主要问题是您未设置map.delegate = self,或者您的视图控制器未继承自MKMapViewDelegate

我也整理了你的代码,下面的代码运行正常。

我还修改了你的ColorPointAnnotation课程,以便更好地工作。

class ColorPointAnnotation: MKPointAnnotation {

    var color: UIColor!
}

查看控制器

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var map: MKMapView!

    var locations = Array<Dictionary<String,Any>>()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Add some test pins

        var attending = Dictionary<String,Any>()

        attending["latitude"] = 40.83

        attending["longitude"] = -73.93

        attending["eventType"] = "No"

        attending["title"] = "Testing Attending"

        self.locations.append(attending)


        var hosting = Dictionary<String,Any>()

        hosting["latitude"] = 37.77

        hosting["longitude"] = -122.45

        hosting["eventType"] = "Yes"

        hosting["title"] = "Testing Hosting"

        self.locations.append(hosting)


        var location = Dictionary<String,Any>()

        location["latitude"] = 33.73

        location["longitude"] = -84.39

        location["title"] = "Testing Default"

        self.locations.append(location)


        // Add test pins to map

        for item in locations {

            add(new: item)
        }
    }

    func add(new location: Dictionary<String,Any>) {

        guard

            let latitude = location["latitude"] as? Double,

            let longitude = location["longitude"] as? Double,

            let title = location["title"] as? String

        else {

            print("Incomplete Data")

            return
        }

        let annotation = ColorPointAnnotation()

        if let eventType = location["eventType"] as? String {

            switch eventType {

            case "No":

                print("Attending Event")

                annotation.color = .red

                break

            case "Yes":

                print("Hosting Event")

                annotation.color = .green

                break

            default :

                // I don't need to set this to purple because if 
                // annotation.color is nil purple will be the default

                break
            }
        }

        annotation.title = title

        annotation.coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

        self.map.addAnnotation(annotation)
    }

    func mapView(_ map: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

        if let pin = annotation as? ColorPointAnnotation {

            let identifier = "pinAnnotation"

            if let view = map.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView {

                view.pinTintColor = pin.color ?? .purple

                return view

            } else {

                let view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)

                view.canShowCallout = true

                view.pinTintColor = pin.color ?? .purple

                return view
            }
        }

        return nil
    }
}

This is how the code above looks.