这应该很容易弄清楚我认为...... 我有一个工作的mapview,它将一系列带编号的pin标记放到地图上。我正在从一个plist调用该引脚的图像,然而,当引脚掉落时,它们都具有与最后一个引脚相同的数字。 (而不是1,2,3 ..)
违规代码如下,我只是不确定如何解决它...谢谢。
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 :(得分:0)
第二个循环遍历所有点(注释),当前注释视图以最后一个点的图像结束。 annotationView对象在该循环内没有改变。
我假设第二个循环和最后一行在viewForAnnotation方法中。
不是循环遍历所有点,而应仅从当前注释对象获取routeDict,并将annotationView的图像设置一次。
假设您在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来存储注释的图像文件名:
@interface AllAnnotations : NSObject <MKAnnotation> {
//existing ivars here
NSString *imageFileName;
}
//existing properties here
@property (copy) NSString *imageFileName;
//existing method headers here
@end
在AllAnnotations.m中,为新的ivar添加合成并更新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;
}