3

正如您在下面看到的,我以基本的方式模拟了我的问题。我有一个定期调用方法的计时器。在这种方法中,我创建了一个 switch-case 条件来模拟我的想法。

一旦在地图上添加大头针,然后再次读取(大头针不断下降)它们。

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

- (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++;
4

2 回答 2

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... 
}
于 2012-09-14T02:45:49.450 回答
0

我认为您需要删除旧注释,因为注释数组是只读的 NSArray *oldAnnotaions = [map annotaions]; [mapView removeAnnotations:oldAnnotations];

于 2012-09-13T22:47:47.617 回答