18

したがって、すべてのピンが追加された MKMapView があり、ピンの色は、そのピンに値が設定されているかどうかに依存します。アプリを最初にロードすると、viewForAnnotationが呼び出され、それに応じて色が設定されます。ただし、ピンの詳細 (場所、タイトルなど) を更新すると、pinColour も更新され、更新されないことがわかります。viewForAnnotation最初の追加後に再度呼び出されないようです。

これに似た多くの質問を読みましたが、それを確認できますmapView.delegate = self;

これが私のviewForAnnotationコードです:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(MapAnnotation *)annotation
{
    if([annotation class] == MKUserLocation.class)
        return nil;

    NSString *pinIdentifier = annotation.identifier; // This is a unique string for each pin and is getting populated every time!

    MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier:pinIdentifier];

    if(annotationView == nil)
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinIdentifier];
    else
        annotationView.annotation = annotation; // Never had this line fire...

    annotationView.canShowCallout = YES;
    annotationView.animatesDrop = NO;
    annotationView.enabled = YES;
    annotationView.tag = annotation.counter;

    if(annotation.pinColour == Stopped) // from enum
        annotationView.pinColor = MKPinAnnotationColorRed;
    else
        annotationView.pinColor = MKPinAnnotationColorGreen;

    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [infoButton addTarget:self action:@selector(mapCalloutButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    infoButton.tag = annotation.counter;
    annotationView.rightCalloutAccessoryView = infoButton;

    return annotationView;
}

ピンを追加するコードは次のとおりです。

CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = latestPosition.latitude;
annotationCoord.longitude = latestPosition.longitude;

MapAnnotation *annotation = [[MapAnnotation alloc] init];
annotation.coordinate = annotationCoord;
annotation.identifier = theIdentifier;
annotation.title = theTitle;
annotation.subtitle = theSubtitle
annotation.pinColour = [self getPinColour];
annotation.counter = theCounter;

[theMapView addAnnotation:annotation];

ピンを更新するコードは次のとおりです(追加する別の方法):

updatePin = true;
pinCounter = mapPin.counter;

CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = latestPosition.latitude;
annotationCoord.longitude = latestPosition.longitude;
[mapPin setCoordinate:annotationCoord];

mapPin.identifier = theIdentifier;
mapPin.subtitle = theSubtitle;
mapPin.pinColour = [self getPinColour];

何が欠けているのかわかりません。viewForAnnotation明らかに機能していますが、最初の追加後に呼び出されることはありません! この関数を呼び出すと、アプリを再起動すると色が変わるため、100% 確実に機能します。

編集:ああ、私は本当に注釈を削除して再度追加したくありません。とにかく短期間でやっていることです!

4

5 に答える 5

46

実際、これがうまくいったかどうかはわかりませんが、これが私がやった方法です。

マップから注釈を削除する必要はありませんでした。必要なことは、パラメーター アノテーションのアノテーション ビューを表示するようにマップに指示することだけです。マップは正しい注釈を返します。そこから、それがアクティブなアイテムであるかどうかを識別するためのカスタム アノテーションのプロパティを用意します。アクティブなアイテムの場合は通常のピン イメージを表示し、それ以外の場合は完全なピン イメージを表示します。

-(void)updateAnnotationImage:(CustomAnnotation *)paramAnnotation
{
    MKAnnotationView *av = [geoMap viewForAnnotation:paramAnnotation];

    if (paramAnnotation.active)
    {
        av.image = [UIImage imageNamed:@"PinNormal.png"];
    }
    else
    {
        av.image = [UIImage imageNamed:@"PinFull.png"];
    }
}

少し遅れましたが、この問題に遭遇した他の人に役立つことを願っています.

于 2012-07-25T06:31:17.733 に答える
17

マップ ビューが注釈をキャッシュする方法により、外観を変更する必要がある場合は、注釈を削除して再度追加する必要があります。簡単な削除と追加が道です。これ以外にキャッシュ無効化メカニズムはありません。

于 2012-05-27T16:24:42.590 に答える