2014-07-02 29 views
14

Bazı çalışma verisi noktalarının başlık ve altyazı öğeleri için ek açıklamaları ayarlamak için bir çalışma döngüm var. Aynı döngü yapısı içinde yapmak istediğim, pim rengini varsayılan yerine Mor olarak ayarlamaktır. Anlayamadığım şey, pimi buna göre ayarlamak için theMapView'ıma girmek için ne yapmam gerektiği.Swift ve MapKit içinde MKPinAnnotationView() kullanarak sıkışmış

Benim uyguladığım döngü ve bir şey de bazı girişimler ...

.... 
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. 

} 

cevap

35

Sen viewForAnnotation temsilci yöntemini uygulamak ve oradan bir MKAnnotationView (veya alt sınıfı) dönmek gerekir.
Bu, Objective-C'de olduğu gibi - temeldeki SDK aynı şekilde çalışır.

Ek açıklamaları ekleyen ve temsilci yöntemini uygulayan for döngüsünden MKPinAnnotationView kopyasını kaldırın. Tks -

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

Parlak: Burada

Swift içinde viewForAnnotation temsilci yönteminin örnek bir uygulamasıdır. Bu işi bıraktım ve işe yaradı. Çok fazla Obj C yapmadığım için bir Swift eşdeğerini görmem gerekiyordu. Derleyicinin satırdaki alt çizgi için bir uyarı attıktan bir küçük nokta: func mapView (_ mapView: MKMapView!, ... Uyarı "Extraneous" _ "parametresinde 'mapView' anahtar kelime argüman adı yok. – Kokanee

+0

@Anna, mevcut bir örnek projeniz var mı? Kafamı duvara karşı vuracağım. Teşekkürler – User4

+0

Yeterli temsilcisi yok yorum yapmak için, ancak yukarıdaki işlev hala pinView! .pinColor amortismana tabi tutulmadan çalışır. PinView okumalıdır! .pinTintColor = UIColor.purpleColor() – Lunarchaos42

İlgili konular