Mapkit多点显示不同的图像

发布于 2024-10-07 06:40:16 字数 1454 浏览 0 评论 0原文

我认为这应该很容易弄清楚...... 我有一个工作地图视图,它将一系列编号的图钉标记放到地图上。我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ˇ宁静的妩媚 2024-10-14 06:40:16

第二个循环遍历所有点(注释),当前注释视图以最后一个点的图像结束。该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];


Edit:
Based on the updated code in your question and comments, here are the changes you need to make.

在 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;
}   

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:

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];


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:

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

In AllAnnotations.m, add the synthesize for the new ivar and update the initWithDictionary and dealloc methods:

@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];
}

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:

- (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;
}   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文