2

このチュートリアルに従おうとしています:http ://www.switchonthecode.com/tutorials/building-an-earthquake-monitor-for-iphone-using-mapkit

アイコンがすべての適切な場所(および適切な番号)に表示されており、画像も表示されています。(ただし、これは常に同じ画像です。これは、条件付きif(self.marker.type == "nzpost"が機能しないためです。これは、typeが常にnullであるためです(initWithAnnotation()内のマーカーオブジェクトのすべてのプロパティも同様です)。 。何らかの理由で、プロパティはすべてnullです。

注釈ビューインターフェイス:

@interface PayMarkerAnnotationView : MKAnnotationView {
    PaymentMarker *marker;
}

@property (strong, nonatomic) IBOutlet PaymentMarker *marker;

@end

注釈付きの初期化:(ここで、self.marker.typeに値を指定する必要があります。

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
    if(self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        NSString* imageName = [[NSString alloc] init];

        if([self.marker.type isEqualToString:@"nzpost"]) {
            imageName = @"icon-map-post.png";
        } else {
            imageName = @"icon-map-incharge.png";
        }
        self.image = [UIImage imageNamed:imageName];
    }
    return self;
}

マップビュー:

- (MKAnnotationView *)mapView:(MKMapView *)lmapView viewForAnnotation:(id <MKAnnotation>)annotation {
    PayMarkerAnnotationView *markerView = (PayMarkerAnnotationView *)[lmapView dequeueReusableAnnotationViewWithIdentifier:@"markerView"];
    if(markerView == nil) {
        markerView = [[PayMarkerAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"markerView"];
    }
    markerView.annotation = annotation;
    return markerView;
}

にマーカーを追加する

-(void)renderPaymentMarkers
{
    [self.map addAnnotations: nzPostMarkers];
    [self.map addAnnotations: inChargeMarkers];
}

Marker class:

@interface PaymentMarker : NSObject <MKAnnotation> {
    NSString* type;
    NSString* description;
    float latitude;
    float longitude;}

@property (nonatomic) NSString* description;
@property (nonatomic) NSString* type;
@property (nonatomic) float latitude;
@property (nonatomic) float longitude;

//MKAnnotation
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

@end
4

1 に答える 1

1

marker変数PayMarkerAnnotationViewが設定されることはありません。

(なぜあなたがそのプロパティIBOutletを同様にしたのかわかりません。)

initWithAnnotationアクセスする前に、メソッドで設定する必要があります。

if (self = [super initWithAnnotation...
    //set marker equal to the annotation this view is being created for...
    marker = annotation;

    self.backgroundColor = [UIColor clearColor];
    ...

markerただし、そもそも変数は必要ありません。を呼び出すと設定されるプロパティが
MKAnnotationViewすでにあります。 annotationsuper initWithAnnotation

だからあなたのinitWithAnnotation中で、これの代わりに:

if([self.marker.type isEqualToString:@"nzpost"]) {

あなたはこれを行うことができます:

PaymentMarker *pm = (PaymentMarker *)self.annotation;
if([pm.type isEqualToString:@"nzpost"]) {

これで主な問題は解決するはずです。


もう1つの潜在的な問題は、ビューが別のアノテーションから再利用されている場合に備えて、 viewForAnnotationビューのプロパティを直接更新していることです。annotation

これは問題ありませんが(elseブロックで実行する必要があります)、問題は、更新時にビューimageも更新されないことannotationです。

したがって、ビューを使用した前の注釈が「nzpost」タイプであったが、現在の注釈が他のタイプである場合、ビューには前の注釈のタイプの画像が引き続き表示されます。

これを修正するには、次の2つの解決策が考えられます。

  • タイプごとに個別のreuseIdentifierを使用します(つまり、「post」と「incharge」の2つのID)。このように、一致する画像を持つビューのみが現在の注釈に再利用されます。
  • とプロパティの両方を上書きsetAnnotation:PayMarkerAnnotationViewて更新します。私はこの解決策を好みます。annotationimage


他の完全に無関係なアイテム...

この行:

NSString* imageName = [[NSString alloc] init];

imageName後でコードが新しい値で上書きされるだけなので、意味がありません。そのため、割り当てられたメモリは破棄されます。代わりに、宣言するだけimageNameです:

NSString* imageName;

これらのプロパティ:

@property (nonatomic) NSString* description;
@property (nonatomic) NSString* type;

属性を使用して宣言する必要がありcopyます(コンパイラはこれについて文句を言う必要があります):

@property (nonatomic, copy) NSString* description;
@property (nonatomic, copy) NSString* type;

descriptionによって提供された同じ名前のプロパティをオーバーライドしているため、プロパティの名前を変更することもできますNSObject(それが意図した場合を除く)。

于 2012-08-23T00:54:28.007 に答える