2014-07-02 10 views
14

Ich habe eine Arbeitsschleife, um Anmerkungen für die Titel- und Untertitelelemente für einige Arbeitsdatenpunkte einzurichten. Was ich in der gleichen Loop-Struktur machen möchte, ist, die Pin-Farbe auf Lila anstatt auf den Standard zu setzen. Was ich nicht herausfinden kann ist, was ich tun muss, um in meine theMapView zu tippen, um den Pin entsprechend einzustellen.Festgefahren mit MKPinAnnotationView() in Swift und MapKit

Meine Arbeits Schleife und einige Versuche an etwas ...

.... 
for var index = 0; index < MySupplierData.count; ++index { 

    // Establish an Annotation 
    myAnnotation = MKPointAnnotation(); 
    ... establish the coordinate,title, subtitle properties - this all works 
    self.theMapView.addAnnotation(myAnnotation) // this works great. 

    // In thinking about PinView and how to set it up I have this... 
    myPinView = MKPinAnnotationView();  
    myPinView.animatesDrop = true; 
    myPinView.pinColor = MKPinAnnotationColor.Purple; 

    // Now how do I get this view to be used for this particular Annotation in theMapView that I am iterating through??? Somehow I need to marry them or know how to replace these attributes directly without the above code for each data point added to the view 
    // It would be nice to have some kind of addPinView. 

} 

Antwort

35

Sie benötigen die viewForAnnotation Delegatmethode und geben eine MKAnnotationView (oder Unterklasse) von dort zu implementieren.
Dies ist genau wie in Objective-C - das zugrunde liegende SDK funktioniert auf die gleiche Weise.

Entfernen Sie die Erstellung von MKPinAnnotationView aus der Schleife for, die die Annotationen hinzufügt und stattdessen die Delegate-Methode implementiert.

Hier ist eine Beispielimplementierung des viewForAnnotation Delegatmethode in Swift:

func mapView(mapView: MKMapView!, 
    viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 

    if annotation is MKUserLocation { 
     //return nil so map view draws "blue dot" for standard user location 
     return nil 
    } 

    let reuseId = "pin" 

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView 
    if pinView == nil { 
     pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
     pinView!.canShowCallout = true 
     pinView!.animatesDrop = true 
     pinView!.pinColor = .Purple 
    } 
    else { 
     pinView!.annotation = annotation 
    } 

    return pinView 
} 
+0

Brilliant - TKS. Ich ließ das direkt fallen und es funktionierte. Ich musste nur ein Swift-Äquivalent sehen, da ich nicht viel Obj C mache. Ein kleiner Punkt in dem der Compiler eine Warnung für die Verwendung des Unterstrichs in der Zeile ausgab: func mapView (_ mapView: MKMapView !, ... Die Warnung war "Extraneous" _ "im Parameter 'mapView' hat keinen Schlüsselwortargumentnamen. – Kokanee

+0

@Anna, hatten Sie ein Beispielprojekt verfügbar? Ich hämmere meinen Kopf gegen die Wand hier. Dank – User4

+0

Ich habe nicht genug rep zu kommentieren, aber die obige Funktion funktioniert immer noch, außer dass pinView! .pinColor nicht abgeschrieben wird. Es sollte pinView lesen! .pinTintColor = UIColor.purpleColor() – Lunarchaos42