1

我想在我的地图上显示注释,然后选择其中一个。

- (void)someWebServiceCallbackWithResult:(NSDictionary *)mapPins {

    [mapView removeAnnotations:mapView.annotations];
    [mapView addAnnotations:[mapPins allValues]];

    for (Pin *p in [mapPins allValues]) {

      if ([p.name isEqualTo:@"Cafe-Derp"] && mapView.hidden == NO) {

        //selectedMapAnnotationView is returned as nil at this point
        MapAnnotationView *selectedMapAnnotationView = (MapAnnotationView *)[mapView viewForAnnotation:p];
        [self presentPopover:p inMapAnnotationView:selectedMapAnnotationView];
        }
    }
}

但是viewForAnnotation:在 if 语句中返回 nil。

这不应该发生,因为addAnnotations:应该会自动显示我地图上的所有图钉。只有在我这样调用之前等待一段时间,该代码才会起作用viewForAnnotation:

double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
  MapAnnotationView *selectedMapAnnotationView = (MapAnnotationView *)[mapView viewForAnnotation:p];
  [self presentPopover:p inMapAnnotationView:selectedMapAnnotationView];
});

为什么会这样?我应该调用地图视图上的方法来触发它刷新吗?

4

1 回答 1

0

我认为你不应该使用viewForAnnotation自己。试试这个

[mapView selectAnnotation:p animated:YES];
于 2013-08-29T00:31:20.980 回答