【问题标题】:MKMapView - viewForAnnotation not called while loading more resultsMKMapView - 加载更多结果时未调用 viewForAnnotation
【发布时间】:2010-03-22 18:37:39
【问题描述】:

编辑添加 * 我还没有找到解决这个问题的方法,有人可以帮忙吗?我的问题与此类似,但发布的答案对我不起作用 - MKMapView refresh after pin moves *End Edit

我有一个启用搜索的地图视图。当用户点击搜索时,我的自定义注释会添加到地图视图中。搜索返回 20 个带有“加载更多”链接的结果。当我点击加载更多时,应该向地图视图添加更多注释。

问题是 - 注释被添加到地图视图中,但没有显示在地图上。如果我放大或缩小以更改地图区域,则会显示图钉。

我发现当我点击“加载更多”时 viewForAnnotations: 没有被调用。我不确定如何触发它。有人吗?

-(void)completedSearch:(NSNotification *)resultNotification
 {
   //begin loop
   //get coordinate
   customAnnotation *annot = [[customAnnotation alloc] initWithCoordinate:coordinate];
   //set title subtitle 
   [annotationsArray addObject:annot];
   //end loop
   [mView addAnnotations:annotationsArray];
   [mView setRegion:region animated:YES];
   [mView regionThatFits:region];
 }

  //custom annotation
  @interface customAnnotation : NSObject <MKAnnotation> 
 { 
   CLLocationCoordinate2D coordinate;  
   NSString*              title; 
   NSString*              info; 
 } 
 @property (nonatomic, retain) NSSting *title;
 @property (nonatomic, retain) NSSting *info;

 -(id) initWithCoordinate:(CLLocationCoordinate2D)c;
 @end

 @implementation customAnnotation
 @synthesize coordinate, title, info;
 -(id) initWithCoordinate:(CLLocationCoordinate2D)c
 {
   coordinate = c;
   return self;
 }
 -(void)dealloc{//dealloc stuff here}
 @end

【问题讨论】:

  • 我假设您正在使用 - (void)addAnnotation:(id)annotation 添加更多注释?让我们看一些代码。
  • 添加代码以显示我如何添加注释
  • 出于性能原因,文档说要立即添加所有注释,因此在某些事件发生(例如滚动)之前,它甚至可能不会尝试重绘。看看,在你添加你的注释之后,在地图视图上调用 setNeedsLayout 是否会强制它重新布局它的注释(并立即调用你的委托方法)。
  • 谢谢,杰森。试过了,没有运气。这真的很奇怪。如果我稍微移动地图,就会出现大头针,但只需单击加载更多,大头针就不会出现。当我点击“加载更多”时,我调用了 setNeedsLayout。不起作用:-(
  • 另外,尝试在地图视图上调用 setNeedsDisplay。无论哪种方式,文档都明确规定在配置地图时设置所有注释。它显然会缓存此信息,并且在地图移动之前不会对其进行更新。 setNeedsDisplay 可能会强制它刷新,但如果不是...

标签: objective-c mkmapview mkannotation mkannotationview


【解决方案1】:

只是想确认一下 我正在从一个线程添加注释 - 这不起作用。 一旦我使用了这个(addCameraIconOnMain 添加我的注释)

[self performSelectorOnMainThread:@selector(addCameraIconOnMain:) withObject:cd waitUntilDone:true];    

成功了!谢谢!

【讨论】:

    【解决方案2】:

    这与接收通知然后添加注释引脚的时间间隔有关。强制它使用主线程添加注释引脚有效。

    【讨论】:

      【解决方案3】:

      就我而言,问题是我在地图委托之前添加了注释。它必须是这样的:

      - (void)viewDidLoad
      {
          ...
      
         // map delegate
         [mView setDelegate:self];
      
         ...
      
         // add annotation
         [_map addAnnotation:_poiAnnotation];
      
         ...
      }
      

      【讨论】:

        猜你喜欢
        • 2012-06-02
        • 1970-01-01
        • 1970-01-01
        • 2012-09-25
        • 1970-01-01
        • 2015-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多