2013-10-24 39 views
1

我在mapview上放置了多個針腳,我需要爲所有針腳顯示不同的圖像,因爲我正在執行下面的代碼。Mapview delegete在ios7中添加註解時未調用

- (void)dropMultiplePinAnnotation:(NSArray *)arrThumbnail 

    { 
     [self removeAnnotations:[self annotations]]; 

     for(Annotation *Annot in arrThumbnail) { 

      self.pinImageName = Annot.PinImage; 
      Annot.coordinate = Detail.coordinate; 
      [self addAnnotation:Annot]; 
      //Till ios6, after this line viewForAnnotation gets called so that I can assign new pin image everytime but in ios7 viewforAnnotation called after this loop ends so it set pin image which was latest saved in self.pinImageName 
     }  
    } 

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

    static NSString *AnnotViewID = @"annotViewID"; 
     MKAnnotationView *annotationView = (MKAnnotationView *)[self dequeueReusableAnnotationViewWithIdentifier:AnnotViewID]; 

     if (annotationView == nil) { 
      annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotViewID]; 
     } 
     annotationView.canShowCallout = NO; 

     annotationView.image = [UIImage imageNamed:self.pinImageName]; 
     annotationView.annotation = annotation; 

     return annotationView; 
    } 

回答

-1

確保爲MKMapView對象設置了MKMapViewDelegate。

你能檢查嗎?

+0

它被調用。所以我們可以從它已經確定 – Heena

1

即使在iOS 6下,你的代碼也不能假定什麼時候調用委託方法,我猜可能會出現這種情況。

爲什麼不只是使用[annotation PinImage]而不是self.pinImageName?傳遞給mapView:viewForAnnotation:的註釋對象與上面傳遞給addAnnotation:的是相同的。

+0

簡短,正確,並重點。 – Anna

0

我在mapview上放置了多個引腳,我需要爲所有引腳顯示不同的圖像,因爲我正在執行下面的代碼。

- (void)dropMultiplePinAnnotation:(NSArray *)arrThumbnail 

    { 
     [self removeAnnotations:[self annotations]]; 

     for(Annotation *Annot in arrThumbnail) { 

      self.pinImageName = Annot.PinImage; 
      Annot.coordinate = Detail.coordinate; 
      [self addAnnotation:Annot]; 
     [_mapView setRegion:MKCoordinateRegionMake(Annot.coordinate,  MKCoordinateSpanMake(0.004,0.004))]; 
      //So you can fire every time viewForAnnotation delegate after every annotation adding to MKMapView 
     }  
    } 

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

    static NSString *AnnotViewID = @"annotViewID"; 
     MKAnnotationView *annotationView = (MKAnnotationView *)[self dequeueReusableAnnotationViewWithIdentifier:AnnotViewID]; 

     if (annotationView == nil) { 
      annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotViewID]; 
     } 
     annotationView.canShowCallout = NO; 

     annotationView.image = [UIImage imageNamed:self.pinImageName]; 
     annotationView.annotation = annotation; 

     return annotationView; 
    }