2010-12-13 82 views
0

這應該是相當容易弄清楚我認爲... 我有一個工作mapview,它將一系列編號引腳標記放到地圖上。我打電話給plist的pin圖像,但是,當引腳掉落時,它們的數量都與最後一個引腳的數量相同。 (而不是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

第二個循環遍歷所有點(註釋),當前註釋視圖以圖像爲最後一點結束。 annotationView對象在該循環內部沒有更改。

我假設第二個循環和最後一行都在viewForAnnotation方法中。

除了循環遍歷所有點,您應該只從當前註釋對象獲取routeDict,並設置annotationView的圖像一次。

假設你已經添加routeDict作爲AllAnnotations類的屬性,你會做這樣的事情:

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,添加伊娃保存圖像文件名註釋:

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

在AllAnnotations.m,添加合成爲新的伊娃和更新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 2010-12-13 20:41:13

+0

您可以確認在您顯示的代碼中:從第一行到第一個for循環是在viewDidLoad中?第二個for循環和之後的行在viewForAnnotation中? – Anna 2010-12-13 20:47:37

+0

是的,這是正確的 – alittlelost 2010-12-13 21:25:58