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

マップに既に追加されている注釈のプロパティを更新する場合は、新しいプロパティを使用して再度追加すると (最初に古いプロパティを削除せずに) 、同じ場所に別の注釈を作成して追加するだけです (新しいプロパティを使用)。 .

ご覧のとおり、 を呼び出すremoveAnnotationaddAnnotation、ちらつきと別のドロップ アニメーションが発生します。

代わりに、既存の注釈への参照を取得してから、そのプロパティを更新する必要があります。

アノテーションが 1 つだけの場合は、クラス レベルの ivar 参照を保持し、値が変更されたときにその参照でプロパティを更新できます。

異なる時期に異なる注釈を更新する必要がある場合、簡単な方法は、マップ ビューのannotations配列で更新する注釈を検索することです。

これには、注釈クラスに、注釈ごとに一意であり、(理想的には) 注釈ごとに一定であるプロパティが必要です。つまり、アノテーションの を更新する場合は、 を「一意の」アノテーション識別子として title使用しないでください。title

代わりに、作成時に各注釈に割り当て、後でその値を使用して注釈を見つけることができるように変更されない別のプロパティ (int または string など) を追加します。

たとえば、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

annotaions 配列は読み取り専用であるため、古い注釈を削除する必要があると思います NSArray *oldAnnotations = [map annotaions]; [mapView removeAnnotations:oldAnnotations];

于 2012-09-13T22:47:47.617 に答える