[英]Can't get different pin image using plist file
我使用plist文件存储在词典中具有名称,地址,坐标和图标(固定图像名称)字符串的注释数据。 我需要根据plist中的图标字符串在地图上用大头针图像显示我的注释。 我循环了注解字典,但从我的所有图钉上的第一个字典开始,它就显示在地图图钉图像上。
我的代码:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
for(path in dict){
NSString *theCategory;
theCategory = [NSString stringWithFormat:@"%@", path];
NSLog(@"%@", path);
NSArray *anns = [dict objectForKey:theCategory];
pinView.image = [UIImage imageNamed:[[anns objectAtIndex:0] objectForKey:@"Icon"]];
}
pinView.canShowCallout=YES;
return pinView;
}
我的plist文件构造:
它显示给我的是:
将为添加的每个注释调用viewForAnnotation
委托方法。
该方法中的for循环将对每个注释以相同的方式运行。 所有的for循环最终都会完成(每次为每个注释添加)将pinView.image
设置pinView.image
for循环读取的最后一项。 这恰好是plist中第一个词典中的第一项。
您需要将pinView.image
设置为要为其viewForAnnotation
的当前注释(即,传递的annotation
参数)的项目的Icon
。 所以,你可以保持对环和检查项匹配annotation
,然后才设置pinView.image
(然后break
了的for循环)。
但是,不断地重新读取和遍历该委托方法中的plist并不是一个好主意。 最好将Icon
设置为注释类的属性,在创建注释时设置属性 (您可能首先遍历plist来创建注释),然后直接在注释对象本身中使用该属性viewForAnnotation
委托方法。
假设您有一些自定义注释类,请将Icon添加为属性:
@interface MyAnnotationClass : NSObject<MKAnnotation>
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, copy) NSString *icon; //<---and @synthesize it in .m
然后在您遍历plist来创建注释的地方,像设置title
属性一样设置icon
属性:
ann.title = [item objectForKey:@"Name"];
ann.icon = [item objectForKey:@"Icon"];
最后,在viewForAnnotation
,您可以直接从annotation
读取icon
属性。 但是首先,您应该检查annotation
是否属于您的自定义类类型(这样用户位置的蓝点不会受到影响,并且可以合理地确定annotation
将具有您将要访问的属性):
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if (![annotation isKindOfClass:[MyAnnotationClass class]])
{
// Note "!" sign in front of above condition.
// Return nil (default view) if annotation is
// anything but your custom class.
return nil;
}
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKAnnotationView *pinView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
// Create an MKAnnotationView instead of MKPinAnnotationView
// because we are setting a custom image.
// Using MKPinAnnotationView sometimes overrides custom image
// with the built-in pin view.
if (pinView == nil)
{
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
pinView.canShowCallout = YES;
}
else
pinView.annotation = annotation;
MyAnnotationClass *myAnn = (MyAnnotationClass *)annotation;
pinView.image = [UIImage imageNamed:myAnn.icon];
// May want to check if myAnn.icon is blank (length == 0)
// (OR if pinView.image is still nil after setting)
// and show some default image in that case otherwise
// annotation will be invisible.
return pinView;
}
顺便说一句,在您的plist文件中, Test3
没有Icon
设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.