iOS7 – 地图视图引脚在触摸屏幕之前不会出现

在我的应用程序中,我使用json从Web服务下载了一组点。 实现iOS7的应用程序,我正在试验这个问题:地点下载,但地图上的引脚不绘,直到用户触摸和“移动”地图。 然后他们出现,并在iOS6中工作。

我怎样才能改正这种行为?

编辑:AddAnnotation被称为在接收数据的方法的结尾,parsingJSON并将它们传递给mylocaction对象:

- (void)plotBarPosition:(NSString *)data_string { // Parse the string into JSON NSDictionary *json = [(NSDictionary*)[datos_string1 JSONValue]objectForKey:@"features"]; for (int i = 0; i < [json count]; i++){ /* PARSING EACH POINT */ MyLocation *location =[[MyLocation alloc] initWithName:nameLoc coordinate:coordinate estado:status antenaId:antenaId]; [_mapView addAnnotation:location]; } } 

我也试过:

 [_mapView performSelectorOnMainThread: @selector(addAnnotations:) withObject: location waitUntilDone: NO]; 

但在这种情况下,注释完全不会出现。

由于这个答案和@RAJA的评论build议,在主线程上调用addAnnotation:addAnnotations: 你可以使用GCD或者performSelectorOnMainThread来做到这一点。

对现有代码进行最less更改的选项是调用addAnnotation:单数):

 - (void)plotBarPosition:(NSString *)data_string { // Parse the string into JSON NSDictionary *json = [(NSDictionary*)[datos_string1 JSONValue]objectForKey:@"features"]; for (int i = 0; i < [json count]; i++){ /* PARSING EACH POINT */ MyLocation *location =[[MyLocation alloc] initWithName:nameLoc coordinate:coordinate estado:status antenaId:antenaId]; //[_mapView addAnnotation:location]; [_mapView performSelectorOnMainThread:@selector(addAnnotation:) withObject:location waitUntilDone:YES]; } } 

或者,要使用addAnnotations:复数),首先将您的注释添加到本地数组,并在一次调用中将它们一起给予地图视图。 以下更改标有>>>

 - (void)plotBarPosition:(NSString *)data_string { // Parse the string into JSON NSDictionary *json = [(NSDictionary*)[datos_string1 JSONValue]objectForKey:@"features"]; //>>> initialize array to hold annotations... NSMutableArray *annotationsToAdd = [NSMutableArray array]; for (int i = 0; i < [json count]; i++){ /* PARSING EACH POINT */ MyLocation *location =[[MyLocation alloc] initWithName:nameLoc coordinate:coordinate estado:status antenaId:antenaId]; //>>> comment out direct addAnnotation... //[_mapView addAnnotation:location]; //>>> add annotation to local array... [annotationsToAdd addObject:location]; } //>>> call addAnnotations: (plural) on main thread... [_mapView performSelectorOnMainThread:@selector(addAnnotations:) withObject:annotationsToAdd waitUntilDone:YES]; } 

有一个协议MKAnnotation – 你必须确定你自己的类的注释与实现这个协议:

 @interface MyAnnotation : NSObject <MKAnnotation> { CLLocationCoordinate2D coordinate; } @property (nonatomic, readonly) CLLocationCoordinate2D coordinate; - (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate; 

然后,您必须启动MyAnnotation并将其添加到地图上:

 MyAnnotation *annotation = [[MyAnnotation alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude, longitude)]; [myMap addAnnotation:annotation]; 

也尝试打电话

 [mapView setNeedsDisplay]; or [mapView setNeedsLayout]; 

添加注释之后