2010-12-13 53 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