0

我有一个简单的地图视图。它有一个方法

-(void)loadAndDisplayPois{
NSLog(@"loadAndDisplayPois");
 if(mapView.annotations.count > 0)
     [mapView removeAnnotations:mapView.annotations];
self.pois = [self loadPoisFromDatabase];
NSLog(@"self.pois.count: %i", self.pois.count);
[mapView addAnnotations:self.pois];
NSLog(@"mapView.annotations.count: %i",mapView.annotations.count);}

在我下载数据并将其保存到数据库之后,该方法被调用,并且我确信该方法会因为日志而被调用。处理下载的类在将数据保存到数据库后执行,[self.senderObj performSelector:@selector(loadAndDisplayPois)];其中 senderObj 是 MapViewController。pois 数组中的计数日志在我第一次单击后显示为 4。但是视图上没有注释,因为viewForAnnotation没有调用(数组中的一个注释(我的当前位置))。在我通过单击 TEST 按钮再次执行该方法后,会在地图上显示所有内容。

在我单击 TEST 按钮viewForAnnotation之后和之后调用该方法。viewWillAppear

从 2 天开始就让我发疯了。我不能了...

4

2 回答 2

0

好吧,我把它缩小了。但我还是不明白。

if(mapView.annotations.count > 0)
    [mapView removeAnnotations:mapView.annotations];
self.pois = [self loadPoisFromDatabase];
[mapView addAnnotations:self.pois];

我通过一个按钮执行此代码,它可以工作。由另一个类调用相同的代码,不起作用。为什么????

区别在哪里?

于 2012-08-29T22:55:51.993 回答
0

您不必手动调用 viewForAnnotation,您必须指出显示地图的类是 MKMapView 的委托

在你的.h

 @interface yourViewController : UIViewController<MKMapViewDelegate> 

编辑:

我在一个项目中使用了这段代码并为我工作:

- (void)updateAnnotations {
    if (self.mapView != nil && self.mapView.annotations != nil) {
        NSArray *annotationsCopy = [NSArray arrayWithArray:[self.mapView.annotations filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"!(self isKindOfClass: %@)", [MKUserLocation class]]]];

        [self.mapView removeAnnotations:annotationsCopy];
    }

    for (int i = 0 ; i < [appDelegate.centrosComerciales count] ; i++) {
        CC *ccAct = (CC *)[appDelegate.centrosComerciales objectAtIndex:i];
        CMapPoint *mp = [[CMapPoint alloc] initWithCC:ccAct];
        [self.mapView addAnnotation:mp];
        [mp release];
    }
} 

我没有删除 mapView.annotations,而是删除了注释的副本,没有用户位置注释。

在 viewWillAppear 上调用 updateAnnotations 方法。

它与您的代码非常相似,但我删除了 annotationsCopy 而不是直接删除了 mapView.annotations。

于 2012-08-29T22:12:27.897 回答