Mapkit多点显示不同的图像
我认为这应该很容易弄清楚...... 我有一个工作地图视图,它将一系列编号的图钉标记放到地图上。我从 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;
}
This should be fairly easy to figure out I think...
I have a working mapview which drops a series of numbered pin markers onto the map. I'm calling the image for the pin from a plist however, when the pins drop they all have the same number that of the last pin. (instead of 1,2,3..)
The offending code is below, i just am not sure how to fix it... thanks.
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第二个循环遍历所有点(注释),当前注释视图以最后一个点的图像结束。该annotationView对象在该循环内没有改变。
我假设第二个循环和最后一行位于 viewForAnnotation 方法中。
您应该仅从当前注释对象获取routeDict并设置annotationView的图像一次,而不是循环遍历所有点。
假设您已将 routeDict 添加为 AllAnnotations 类中的属性,您将执行以下操作:
Edit:
Based on the updated code in your question and comments, here are the changes you need to make.
在 AllAnnotations.h 中,添加 ivar 来存储注释的图像文件名:
在 AllAnnotations.m 中,添加新 ivar 的合成并更新 initWithDictionary 和 dealloc 方法:
或者,您可以存储 POINT_KEY 和 COLOR_KEY 的值注释中的对象并在 viewForAnnotation 方法中生成文件名。
顺便说一句,“AllAnnotations”对于代表单个注释的此类来说并不是一个好名字。也许“RouteAnnotation”会更好。
最后,viewForAnnotation 方法应如下所示:
The second loop goes through all the points (annotations) and the current annotation view ends up with the image for the last point. The annotationView object is not changing inside that loop.
I assume that second loop and the last line are in the viewForAnnotation method.
Instead of looping through all points, you should get the routeDict from the current annotation object only and set the annotationView's image once.
Assuming you've added routeDict as a property in the AllAnnotations class, you would do something like this:
Edit:
Based on the updated code in your question and comments, here are the changes you need to make.
In AllAnnotations.h, add an ivar to store the image file name for the annotation:
In AllAnnotations.m, add the synthesize for the new ivar and update the initWithDictionary and dealloc methods:
Alternatively, you could store the values of the POINT_KEY and COLOR_KEY objects in the annotation and generate the filename in the viewForAnnotation method.
By the way, "AllAnnotations" is not a good name for this class which represents a single annotation. Perhaps "RouteAnnotation" would be better.
Finally, the viewForAnnotation method should look like this: