【问题标题】:Different custom image for each pin每个引脚都有不同的自定义图像
【发布时间】:2014-08-07 14:10:42
【问题描述】:

这听起来像是一个笼统的问题,但我只有在我想要我的所有图钉都使用相同的图像时才找到答案,而我不需要。

这就是我现在的工作方式:

我的所有位置都在一个 Locations 数组中(具有长、纬度、名称、引脚名称的自定义类)。

在 viewdidLoad 中,我循环该数组并使用找到的每个对象创建我的引脚,请参见以下代码:

     for(int i = 0 ; i<[_locationList count] ; i++)
        {
        Location *item = [_locationList objectAtIndex:i];
        CLLocationCoordinate2D coordinate;
        coordinate.latitude = item.locationLatitude;
        coordinate.longitude = item.locationLongitude;
        NSString *title = item.locationName;
        CustomAnnotation* ann = [CustomAnnotation new];
        ann.name = title;
        ann.coordinate = coordinate;
        ann.pinName = [NSString stringWithFormat:@"pin%i.png",item.idPin];
        [self.map addAnnotation:ann];
    }

这很简单,来自 CustomAnnotation 类,代码如下:

@interface CustomAnnotation : MKPointAnnotation <MKAnnotation>{

}

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *description;
@property (nonatomic, retain) NSString *pinName;


@end

这都是我在互联网上看到的东西,我有点相信到目前为止都是正确的。

在我看来,我仍在创建非常经典的 pin,它们只是多了一个属性 (pinName),这就是它来自自定义类的原因。

现在,在 viewForAnnotation 中,我完全不知道如何告诉它获取该 pinName 并使用它。

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    // If it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    // Handle any custom annotations.
    if ([annotation isKindOfClass:[MKPointAnnotation class]])
    {
        // Try to dequeue an existing pin view first.
        MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
        if (!pinView)
        {
            // If an existing pin view was not available, create one.
            pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
            pinView.canShowCallout = YES;
            pinView.image = // I wanted to write annotation.pinName But it's just not that.
            pinView.calloutOffset = CGPointMake(0, 32);

             }else {
            pinView.annotation = annotation;
        }
        return pinView;
    }
    return nil;
}

我错过了什么?我显然做错了什么,但我无法弄清楚,我仍然对 MKAnnotationsView & MKPointAnnotation & MKPinAnnotation 之间的区别感到很困惑,...

更多信息:引脚名称是“pinX.png”,X 是 1 到 12 之间的数字。我只想使用该名称,以便程序可以在图片所在的资源中找到它。

【问题讨论】:

    标签: ios mapkit mkannotationview


    【解决方案1】:

    由于您的注释属于CustomAnnotation 类型,因此检查注释是否属于该类型而不是MKPointAnnotation 会更准确。

    然后,将annotation 参数转换为您的自定义类将让您轻松访问其中的自定义属性,如下所示:

    CustomAnnotation *ca = (CustomAnnotation *)annotation;
    pinView.image = [UIImage imageNamed:ca.pinName];
    

    但是,由于每个注释的图像可能不同,您应该在 创建注释视图之后设置它 出列(不仅在if (!pinView)块 -- 否则注释可能最终会重新使用另一个不再可见的注释中的视图,并且会显示错误的图像。

    更新后的方法如下所示:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
        // If it's the user location, just return nil.
        if ([annotation isKindOfClass:[MKUserLocation class]])
            return nil;
    
        // Handle any custom annotations.
        if ([annotation isKindOfClass:[CustomAnnotation class]])
        {
            // Try to dequeue an existing pin view first.
            MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
            if (!pinView)
            {
                // If an existing pin view was not available, create one.
                pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
                pinView.canShowCallout = YES;
    
                pinView.calloutOffset = CGPointMake(0, 32);
                //NOTE:
                //If the calloutOffset needs to be different for each image,
                //then this line should also be set below with the image.
            }
            else {
                pinView.annotation = annotation;
            }
    
            //Set image on view AFTER we have a new or dequeued view
            //because image is based on each annotation...
    
            CustomAnnotation *ca = (CustomAnnotation *)annotation;
            pinView.image = [UIImage imageNamed:ca.pinName];
    
            //Might want to check that the UIImage is not nil
            //in case pinName is invalid since that would result in
            //an invisible annotation view.  If the UIImage is nil,
            //set pinView.image to some default image.
    
            return pinView;
        }
        return nil;
    }
    


    有关 MapKit 类之间差异的混淆,请参阅:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-20
      • 2011-12-31
      相关资源
      最近更新 更多