2010-12-13 5 views
0

これはかなりわかりやすいはずです。 マップ上にピン番号の付いた一連のピンマーカーが表示されます。私はplistからピンのイメージを呼んでいますが、ピンが落ちると、それらはすべて最後のピンの番号と同じ番号になります。 (1,2,3 ..の代わりに)mapkitの複数のポイントが異なる画像で表示される

違反コードは以下の通りです。私はそれを修正する方法がわかりません...ありがとうございます。

NSString *path = [[NSBundle mainBundle] pathForResource:[route objectForKey:NAME_KEY] ofType:@"plist"]; 
NSMutableArray* array = [[NSMutableArray alloc] initWithContentsOfFile:path]; 
points = array; 

for (NSDictionary *routeDict in points){ 
AllAnnotations *Annotation = [[AllAnnotations alloc] initWithDictionary:routeDict]; 
[mapView addAnnotation:Annotation]; 
[Annotation release]; 
} 



    for (NSDictionary *routeDict in points){ 
    NSString *first = [routeDict objectForKey: POINT_KEY]; 
    NSString *getNum = [routeDict objectForKey: COLOR_KEY]; 
    NSString *again = [getNum stringByAppendingString:first]; 
    NSString *imgValue = [again stringByAppendingString:@".png"]; 
    annotationView.image = [UIImage imageNamed:imgValue]; 
    } 

    annotationView.canShowCallout = YES; 

AllAnnotations.m 
------------------------------------------------------------------------------------------ 
- (id) initWithDictionary:(NSDictionary *) dict 
{ 
    self = [super init]; 
    if (self != nil) { 
     coordinate.latitude = [[dict objectForKey:@"latitude"] doubleValue]; 
     coordinate.longitude = [[dict objectForKey:@"longitude"] doubleValue]; 
     self.title = [dict objectForKey:@"name"]; 
     self.subtitle = [dict objectForKey:@"subname"]; 

    } 
    return self; 
} 

答えて

0

2番目のループはすべてのポイント(注釈)を通過し、現在の注釈ビューは最後のポイントの画像で終了します。 annotationViewオブジェクトはそのループ内で変更されていません。

2番目のループと最後の行がviewForAnnotationメソッドにあるとします。

すべての点をループする代わりに、現在の注釈オブジェクトからのみrouteDictを取得し、注釈ビューの画像を1回設定する必要があります。

あなたはAllAnnotationsクラスのプロパティとしてrouteDictを追加したと仮定すると、あなたはこのような何かをするだろう:

NSDictionary *routeDict = ((AllAnnotations *)annotation).routeDict; 
NSString *first = [routeDict objectForKey: POINT_KEY]; 
NSString *getNum = [routeDict objectForKey: COLOR_KEY]; 
NSString *again = [getNum stringByAppendingString:first]; 
NSString *imgValue = [again stringByAppendingString:@".png"]; 
annotationView.image = [UIImage imageNamed:imgValue]; 


編集:あなたの質問やコメントで更新されたコードに基づいて
、ここで必要な変更があります。

AllAnnotations.hで

、注釈用の画像ファイル名を格納するためのIVARを追加します。AllAnnotations.mで

@interface AllAnnotations : NSObject <MKAnnotation> { 
    //existing ivars here 
    NSString *imageFileName; 
} 
//existing properties here 
@property (copy) NSString *imageFileName; 
//existing method headers here 
@end 

、新しいIVARのためのsynthesizeを追加し、initWithDictionaryとのdeallocメソッドを更新します。

@synthesize imageFileName; 

- (id) initWithDictionary:(NSDictionary *) dict 
{ 
    self = [super init]; 
    if (self != nil) { 
     coordinate.latitude = [[dict objectForKey:@"latitude"] doubleValue]; 
     coordinate.longitude = [[dict objectForKey:@"longitude"] doubleValue]; 
     self.title = [dict objectForKey:@"name"]; 
     self.subtitle = [dict objectForKey:@"subname"]; 

     //set this annotation's image file name... 
     NSString *first = [dict objectForKey: POINT_KEY]; 
     NSString *getNum = [dict objectForKey: COLOR_KEY]; 
     NSString *again = [getNum stringByAppendingString:first]; 
     self.imageFileName = [again stringByAppendingString:@".png"]; 
    } 
    return self; 
} 

- (void) dealloc 
{ 
    [title release]; 
    [subtitle release]; 
    [imageFileName release]; 
    [super dealloc]; 
} 

また、注釈にPOINT_KEYおよびCOLOR_KEYオブジェクトの値を格納し、viewForAnnotationメソッドでファイル名を生成することもできます。

ところで、「AllAnnotations」は単一の注釈を表すこのクラスの適切な名前ではありません。おそらく "RouteAnnotation"が良いでしょう。

最後に、viewForAnnotation方法は次のようになります。私は間違いなく近づいていますが、私はまだ少し迷ってしまいました

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    MKAnnotationView * annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"annot"]; 
    if (!annotationView) { 
     annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annot"] autorelease]; 
     annotationView.canShowCallout = YES; 
    } 
    else { 
     annotationView.annotation = annotation; 
    } 

    AllAnnotations *routeAnnotation = (AllAnnotations *)annotation; 
    annotationView.image = [UIImage imageNamed:routeAnnotation.imageFileName]; 

    return annotationView; 
} 
+0

感謝を。私はAllAnnotations.mファイルを追加するために上で編集しました...辞書はプロパティとして追加されません。私はこれを試してみると空白のマップが得られるので、Annotations.mファイルに追加する必要があるものは何か分かりません。 – alittlelost

+0

あなたが示したコードで、最初の行から最初のfor-loopまでがviewDidLoadにあることを確認できますか? 2番目のfor-loopとそれ以降の行はviewForAnnotationにありますか? – Anna

+0

はい、それは正しいです – alittlelost

関連する問題