Ich bin daran interessiert, die Pinfarbe einer Annotation basierend auf einem Fall zu ändern. In einer Funktion habe ich ein Array gesendet, das bestimmt, welche Farbe die Pin-Annotation hat. Bis jetzt habe ich eine Unterklasse namens ColorPointAnnotation festgelegt, die die PinColor bestimmen wird. Dann setze ich in der switch-Anweisung die ColorPointAnnotation für die Annotation. Und in der viewForAnnotation platziere ich die neue Anmerkung mit der Farbe. Nach meinem Verständnis von Anmerkungen aus der Dokumentation ist das alles, was benötigt wird, jedoch ändert sich die Pin-Farbe auf der mapView nicht. Ich bin mir nicht sicher, was das Problem sein könnte.Swift 3.0 Pin Color Annotation ändert sich nicht in 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
}
Ja, es war, weil ich die Karte nicht delegieren .. so ein dummer Fehler gesetzt haben! Danke auch für die Vereinfachung des Codes! – Kevin