2012-06-05 14 views
5

मेरे पास 200 से अधिक वस्तुओं के साथ एक सरणी है और मैं उनमें से प्रत्येक के माध्यम से एक लूप करने की कोशिश कर रहा हूं।विभिन्न पिन रंगों के साथ मानचित्र दृश्य एनोटेशन

प्रत्येक ऑब्जेक्ट में हां/नहीं फ़ील्ड होगा और मैं उस हां/नहीं मान पर निर्भर एक अलग रंगीन मार्कर प्रदर्शित करना चाहता हूं।

जो मैं देख सकता हूं उससे मेरा लूप पहले प्रत्येक ऑब्जेक्ट के माध्यम से जा रहा है और फिर प्रत्येक ऑब्जेक्ट के लिए अंत में सभी एनोटेशन जोड़ा जाता है।

जब से मैं जब सभी एनोटेशन मेरे मानचित्र में जोड़े हाँ कोई मूल्य पर सरणी के माध्यम से अपने पाश के भीतर एक जांच करते हैं, यह हाँ का उपयोग करेगा/सरणी में पिछले वस्तु से कोई मूल्य नहीं है जब यह साजिश को जाता है सबके लिए।

मैं यह कैसे कर सकता हूं ताकि मार्कर प्रत्येक व्यक्तिगत तत्व के लिए हाँ/नहीं मान पर निर्भर हो?

मेरे कोड के रूप में इस प्रकार है

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{ 

    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"]; 

if(current_yesno == YES){ 
    annView.pinColor = MKPinAnnotationColorGreen; 
} 
else 
{ 
    annView.pinColor = MKPinAnnotationColorRed; 
} 
    annView.animatesDrop=NO; 
    annView.canShowCallout = YES; 
    annView.calloutOffset = CGPointMake(-5, 5); 
    return annView; 

} 

और current_yesno मेरी ज फ़ाइल में घोषित किया जाता है मेरी टिप्पणी कोड के साथ

for (i = 0; i < [appDelegate.itemArray count]; i++) { 
     item_details *tempObj = [appDelegate.itemArray objectAtIndex:i]; 
     location.latitude = [tempObj.lat floatValue]; 
     location.longitude = [tempObj.lon floatValue]; 
     current_yesno = tempObj.yesno; 
     MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc]initWithTitle:tempObj.name andCoordinate:location]; 
     [self.mapView addAnnotation:newAnnotation]; 
     [newAnnotation release];    
      } 

है।

उत्तर

8

viewForAnnotation प्रतिनिधि विधि जरूरी नहीं बुलाया जाता है आप addAnnotation करते हैं और यह भी मानचित्र दृश्य जब यह (जबकि अपने कोड कुछ पूरी तरह से अलग कर रहा है एक एनोटेशन के लिए दृश्य प्राप्त करने की जरूरत है द्वारा अन्य समय में कहा जा सकता है के तुरंत बाद)।

तो आप उस प्रतिनिधि विधि के बाहर कुछ कोड के साथ सिख में रहने वाले ivar के मूल्य पर निर्भर नहीं हो सकते हैं।

इसके बजाय, अपने कस्टम MapViewAnnotation वर्ग के लिए yesno प्रॉपर्टी जोड़ते हैं, यह निर्धारित करने पर एनोटेशन बनाने और फिर annotation पैरामीटर के माध्यम से viewForAnnotation में अपने मूल्य का उपयोग (यानी। मानचित्र दृश्य आप सटीक व्याख्या वस्तु के लिए एक संदर्भ दे रहा है यह के लिए दृश्य चाहता है)।

उदाहरण:

MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] init... 
newAnnotation.yesno = tempObj.yesno; // <-- set property in annotation 
[self.mapView addAnnotation:newAnnotation]; 

फिर viewForAnnotation में:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation 
{ 
    if (![annotation isKindOfClass:[MapViewAnnotation class]]) 
    { 
     // Return nil (default view) if annotation is 
     // anything but your custom class. 
     return nil; 
    } 

    static NSString *reuseId = @"currentloc"; 

    MKPinAnnotationView *annView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId]; 
    if (annView == nil) 
    { 
     annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];   
     annView.animatesDrop = NO; 
     annView.canShowCallout = YES; 
     annView.calloutOffset = CGPointMake(-5, 5); 
    } 
    else 
    { 
     annView.annotation = annotation; 
    } 

    MapViewAnnotation *mvAnn = (MapViewAnnotation *)annotation; 
    if (mvAnn.yesno) 
    { 
     annView.pinColor = MKPinAnnotationColorGreen; 
    } 
    else 
    { 
     annView.pinColor = MKPinAnnotationColorRed; 
    } 

    return annView; 
} 
+0

उत्तर के लिए धन्यवाद। मैं संपत्ति Yano toMapViewAnnotation कैसे जोड़ूं? – user1096447

+1

MapViewAnnotation.h में, '@property (nonatomic, असाइन करें) BOOL yesno;' और MapViewAnnotation.m में '@ सिंथेसाइज़ yesno डालें; ' – Anna

+0

धन्यवाद अन्ना, एक प्रासंगिक उत्तर दें। –

0
MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier: @"id"]; 
if (pin == nil) 
{ 
    pin = [[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"id"] ; 
} 
else 
{ 
    pin.annotation = annotation; 
} 

pin.pinTintColor=[UIColor blueColor]; 
pin.canShowCallout = true; 
संबंधित मुद्दे