iOS7 - 触摸屏幕前不会显示地图视图引脚

时间:2014-02-17 23:16:25

标签: ios ios7 mkmapview

在我的应用程序中,我使用json从Web服务下载一组点。 将应用程序实现到iOS7,我正在尝试解决这个问题:下载位置,但是在用户触摸并“移动”地图之前,地图上的图钉不会被绘制。然后它们出现并且都像iOS6一样工作。

我该如何纠正这种行为?

编辑: 在接收数据的方法的末尾调用AddAnnotation,解析json并将它们传递给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];

但在这种情况下,注释根本不会出现。

2 个答案:

答案 0 :(得分:2)

this answer和@RAJA的评论建议,请在主线程上调用addAnnotation:addAnnotations:。您可以使用GCD或performSelectorOnMainThread

执行此操作

对现有代码进行最少更改的选项是调用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];
}

答案 1 :(得分:0)

有一个协议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];
添加注释后