2014-07-02 9 views
14

के भीतर MKPinAnnotationView() का उपयोग करने पर अटक गया मेरे पास कुछ कामकाजी डेटा बिंदुओं के लिए शीर्षक और उपशीर्षक तत्वों के लिए एनोटेशन सेट करने के लिए एक कार्यशील लूप है। मैं उसी लूप संरचना के भीतर क्या करना चाहता हूं वह पिन रंग को डिफ़ॉल्ट के बजाय बैंगनी पर सेट करना है। मैं समझ नहीं पा रहा हूं कि पिन को तदनुसार सेट करने के लिए मुझे अपने मैप व्यू में टैप करने के लिए क्या करना है।स्विफ्ट और मैपकिट

मेरे काम के पाश और कुछ पर कुछ प्रयास ...

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

} 

उत्तर

35

आप viewForAnnotation प्रतिनिधि विधि को लागू करने और वहाँ से एक MKAnnotationView (या उपवर्ग) लौटना ही होगा।
यह उद्देश्य-सी में है - अंतर्निहित एसडीके वैसे ही काम करता है।

MKPinAnnotationViewfor लूप से निर्माण को हटा दें जो एनोटेशन जोड़ता है और इसके बजाय प्रतिनिधि विधि को कार्यान्वित करता है। 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

शानदार:

यहाँ स्विफ्ट में viewForAnnotation प्रतिनिधि विधि का एक नमूना कार्यान्वयन है। मैंने यह अधिकार गिरा दिया और यह काम किया। मुझे बस एक स्विफ्ट समकक्ष देखने की ज़रूरत है क्योंकि मैं बहुत सारे ओब्जे सी नहीं करता हूं। एक मामूली बात यह है कि संकलक ने लाइन में अंडरस्कोर का उपयोग करने के लिए चेतावनी फेंक दी: func mapView (_ mapView: MKMapView!, ... चेतावनी "अपरिपक्व" _ "पैरामीटर में है" नक्शा व्यू में कोई कीवर्ड तर्क नाम नहीं है। – Kokanee

+0

@ अन्ना, क्या आपके पास एक उदाहरण प्रोजेक्ट उपलब्ध है? मैं यहां दीवार के खिलाफ अपने सिर को टक्कर लगी हूं। धन्यवाद – User4

+0

मेरे पास पर्याप्त प्रतिनिधि नहीं है टिप्पणी करने के लिए, लेकिन उपर्युक्त कार्य अभी भी काम करता है सिवाय इसके कि पिनव्यू!। पिनकॉलर को कम नहीं किया गया है। इसे पिन व्यू पढ़ना चाहिए !pinTintColor = UIColor.purpleColor() – Lunarchaos42

संबंधित मुद्दे