在地图视图上重新添加注释以更新其标题

cas*_*las 3 mkmapview mkannotation ios

如下所示,我以基本方式模拟了我的问题.我有一个定期调用方法的计时器.在那种方法中,我创建了一个切换案例条件来模拟我的想法.

一旦在地图上添加了引脚,然后再次读取(引脚不断丢失)它们.

我想添加我的图钉,然后只更改代表天气价值的标题.

- (IBAction)playBtn:(id)sender {

     timer = [NSTimer scheduledTimerWithTimeInterval:(4.0) target:self selector:@selector(control) userInfo:nil repeats:YES];

}

-(void)control{
    NSMutableArray *annotationArray = [[[NSMutableArray alloc] init] autorelease];

    switch (value%2) {
        case 0:
        {

    // Create some annotations
    Annotation *annotation = nil;

    annotation = [[Annotation alloc] init];
    annotation.coordinate = CLLocationCoordinate2DMake(29.7161,-95.3906);
    annotation.color = RGB(13, 0, 182);
    annotation.title = @"17";
    [annotationArray addObject:annotation];
    [annotation release];

    annotation = [[Annotation alloc] init];
    annotation.coordinate = CLLocationCoordinate2DMake(30.168456,-95.504480);
    annotation.color = RGB(0, 182, 146);
    annotation.title = @"16";
    [annotationArray addObject:annotation];
    [annotation release];


    // Center map
    //self.mapView.visibleMapRect = [self makeMapRectWithAnnotations:annotationArray];

    // Add to map
    //[self.mapView addAnnotations:annotationArray];
        }
            break;
        case 1:
        {
            // Create some annotations
            Annotation *annotation = nil;

            annotation = [[Annotation alloc] init];
            annotation.coordinate = CLLocationCoordinate2DMake(29.7161,-95.3906);
            annotation.color = RGB(13, 0, 182);
            annotation.title = @"27";
            [annotationArray addObject:annotation];
            [annotation release];

            annotation = [[Annotation alloc] init];
            annotation.coordinate = CLLocationCoordinate2DMake(30.168456,-95.504480);
            annotation.color = RGB(0, 182, 146);
            annotation.title = @"25";
            [annotationArray addObject:annotation];
            [annotation release];

        }
            break;
    }
    [self.mapView addAnnotations:annotationArray];
    [mapView setNeedsDisplay];
    value++;
Run Code Online (Sandbox Code Playgroud)

小智 7

如果要更新已添加到地图的注释的属性,请使用新属性再次添加(不先删除旧属性),只需在同一位置创建并添加另一个注释(使用新属性) .

如你所见,调用removeAnnotation然后addAnnotation导致闪烁和另一个下拉动画.

相反,您必须获得对现有注释的引用,然后更新其属性.

如果它只是一个注释,您可以保留对它的类级别ivar引用,并在值更改时使用该引用更新属性.

如果需要在不同时间更新不同的注释,一种简单的方法是在地图视图的annotations数组中搜索要更新的注释.

这要求您在注释类中具有一个属性,该属性对于每个注释都是唯一的,并且(理想情况下)对于每个注释保持不变.换句话说:如果您要更新注释title,请不要将其title用作"唯一"注释标识符.

相反,添加在创建每个注释时分配给每个注释的另一个属性(例如,int或字符串),并且不会更改,以便稍后可以使用该值找到注释.

例如,假设您添加了一个调用annotationId到注释类的int属性,并且您想要更新ID为#42的注释:

BOOL annFound = NO;

//loop through the map view's annotations array to find annotation id# 42...
for (id<MKAnnotation> ann in mapView.annotations)
{
    if ([ann isKindOfClass:[MyAnnotationClass class]])
    {
        MyAnnotationClass *myAnn = (MyAnnotationClass *)ann;
        if (myAnn.annotationId == 42)
        {
            annFound = YES;
            myAnn.title = @"some new title";
            break;
        }
    }
}

if (!annFound)
{
    //annotation id# 42 is not yet on the map.
    //create it and add to map... 
}
Run Code Online (Sandbox Code Playgroud)