【问题标题】:Adding Multiple Annotation Images添加多个注释图像
【发布时间】:2014-07-29 18:39:43
【问题描述】:

在这个上有点磕磕绊绊,我在这个上已经用尽了很多小时。我查看了其他几个问题和答案,但似乎无法解决这个问题。现在代码只是使所有注释都相同,这是我数组中最后一个图像。基本上,我想根据字典中的图像更改每个图钉的注释图像。因此,地图上的所有 60 个图钉将是不同的图像。这是我设置注释的方法:

//Remove any existing custom annotations but not the user location blue dot.
for (id<MKAnnotation> annotation in mapView.annotations)
{
    if ([annotation isKindOfClass:[MapPoint class]])
    {
        [mapView removeAnnotation:annotation];
    }
}

 NSArray *dict = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]  pathForResource:@"FastFood" ofType:@"plist"]];


//Access name value in dictionary, store to array
//  NSArray *nameFromPlist = [dict valueForKey:@"name"];
//Loop through the array of places returned from the Google API.
for (int i=0; i<[data count]; i++)
{
    int currentIndex = -1;

    for (NSDictionary *plistname in [dict valueForKey:@"name"])//nameFromPlist[@"name"])
  {
      currentIndex++;

    NSPredicate *inBothLists=[NSPredicate predicateWithFormat:@"Self CONTAINS[c] %@", plistname];
      BOOL result = [inBothLists evaluateWithObject:[[data objectAtIndex:i] objectForKey:@"name"]];

      if (result == YES)
      {
    //Retrieve the NSDictionary object in each index of the array.
    NSDictionary* place = [data objectAtIndex:i];

    NSDictionary* image = [dict objectAtIndex:currentIndex];

    //There is a specific NSDictionary object that gives us location info.
    NSDictionary *geo = [place objectForKey:@"geometry"];

          imgRestLogo = nil;
          imgRestLogo = [UIImage imageNamed:[image objectForKey:@"image"]];
        //  NSString *imgtest= [image objectForKey:@"image"];
       //   NSLog(@"Image Filename is: %@ " , imgtest);

    //Get our name and address info for adding to a pin.
    NSString *name=[place objectForKey:@"name"];
    NSString *vicinity=[place objectForKey:@"vicinity"];

    //Get the lat and long for the location.
    NSDictionary *loc = [geo objectForKey:@"location"];

    //Create a special variable to hold this coordinate info.
    CLLocationCoordinate2D placeCoord;

    //Set the lat and long.
    placeCoord.latitude=[[loc objectForKey:@"lat"] doubleValue];
    placeCoord.longitude=[[loc objectForKey:@"lng"] doubleValue];

    //Create a new annotiation.
    MapPoint *placeObject = [[MapPoint alloc] initWithName:name address:vicinity    coordinate:placeCoord];


    [mapView addAnnotation:placeObject];


      }

这是我的 viewForAnnotation 方法:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {

//Define our reuse indentifier.
static NSString *identifier = @"MapPoint";


if ([annotation isKindOfClass:[MapPoint class]]) {

    MKAnnotationView *annotationView = (MKAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];

    } else {
        annotationView.annotation = annotation;
    }

    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;

UIImage *image = imgRestLogo; //[UIImage imageNamed:@"GMPin.png"];
annotationView.image = image;

    return annotationView;
}

return nil;
}

提前致谢!

【问题讨论】:

  • 为什么 dict 是 NSArray* ?如果它真的是一个 NSArray,你为什么要在上面调用valueForKey:
  • plist 是一个字典数组。我之所以调用 valueForKey,是因为我正在为每个对象获取“名称”的值。代码按预期工作,除了添加注释。所有引脚都有相同的图像,这是我数组中的最后一个图像。
  • 您不能假设 viewForAnnotation 会在每次 addAnnotation 完成后立即调用一次。您必须将imgRestLogo 设置为注释本身的属性。见stackoverflow.com/questions/24215210/…stackoverflow.com/questions/19514494/…等。
  • 谢谢你,安娜,我正在审查那个帖子,看看我能不能弄清楚!我可能很难将 imgRestLogo 设置为属性,但也许我会从示例中理解。
  • 我正在查看这两个帖子以及您就 mkannotations 给出的其他答案,Anna。你真是个专家。我仍然有这个问题,部分原因是我不是objective-c专家。我已经接触了一年,而且对面向对象的概念也很陌生。你能在这方面进一步帮助我吗?我将不胜感激。

标签: ios objective-c mkmapview mkannotation


【解决方案1】:

我的建议是首先使用 imageName 属性对 MKAnnotation 进行子类化,假设您有一个名为 MYAnnotation 的类



    @interface MYAnnotation : NSObject 
    @property (copy, nonatomic) NSString *title;
    @property (copy, nonatomic) NSString *subtitle;
    @property (copy, nonatomic) NSString *imageName;
    @end

然后添加您的图像名称。 (注:图片名的获取方式根据你的plist文件的结构而定。)



    NSDictionary *imageName = [dict objectAtIndex:currentIndex];
    annotation.imageName = imageName;

在委托中,通过以下方式设置图片。


    MKAnnotationView *annotationView = (MKAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annotationView == nil) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    } else {
        annotationView.annotation = annotation;
    }

    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;

    NSString *imageName = ((MYAnnotation *)annotation).imageName;
    UIImage *image = [UIImage imageNamed:imageName];
    annotationView.image = image;

    return annotationView;

【讨论】:

    猜你喜欢
    • 2017-10-30
    • 2021-10-28
    • 2014-07-22
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 2017-12-25
    相关资源
    最近更新 更多