【问题标题】:Mapview delegate not calling when add Annotation in ios7在 ios7 中添加注释时 Mapview 委托未调用
【发布时间】:2013-10-24 11:26:30
【问题描述】:

我在 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];
        //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;
}

【问题讨论】:

    标签: iphone objective-c ios7 mkmapview


    【解决方案1】:

    您的代码无法假设调用该委托方法的时间,即使在 iOS 6 下,我猜想在某些情况下这会中断。

    为什么不直接使用[annotation PinImage] 而不是self.pinImageName?传递给mapView:viewForAnnotation: 的注解对象与上面传递给addAnnotation: 的注解对象相同。

    【讨论】:

      【解决方案2】:

      我在 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;
          }
      

      【讨论】:

        【解决方案3】:

        确保为 MKMapView 对象设置了 MKMapViewDelegate。

        你能检查一下吗?

        【讨论】:

        • 它被调用了。这样我们就可以识别它已经被设置了
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-06-11
        • 1970-01-01
        • 2016-01-22
        • 1970-01-01
        • 1970-01-01
        • 2013-11-16
        • 1970-01-01
        相关资源
        最近更新 更多