OGeek|极客世界-中国程序员成长平台

标题: ios - 每个引脚都有不同的自定义图像 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:01
标题: ios - 每个引脚都有不同的自定义图像

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

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

我的所有位置都在一个 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 *)mapViewMKMapView *)mapView viewForAnnotationid <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 之间的数字。我只想使用该名称,以便程序可以在图片所在的资源中找到它。



Best Answer-推荐答案


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

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

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

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

更新后的方法如下所示:

- (MKAnnotationView *)mapViewMKMapView *)mapView viewForAnnotationid <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 类之间差异的混淆,请参阅:

关于ios - 每个引脚都有不同的自定义图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25184753/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4