ios - 每个引脚都有不同的自定义图像

标签 ios mapkit mkannotationview

这听起来像是一个通用问题,但只有当我想要所有引脚使用相同的图像时我才找到答案,但我不这样做。

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

我的所有位置都位于位置数组中(带有长、纬度、名称、引脚名称的自定义类)。

在 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

这全部来 self 在互联网上看到的内容,我相信到目前为止都是正确的。

在我看来,我仍在创建非常经典的引脚,它们只是多了一个属性 (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 之间的数字。我只想使用该名称,以便程序可以在图片所在的资源中找到它。

最佳答案

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

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

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

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

更新后的方法如下所示:

- (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类之间的差异的困惑,请参见:

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

相关文章:

ios - MKPointAnnotation 标签

ios - 根据所选的 MKAnnotationView 动态更改 leftCalloutAccessoryView

ios - 使用动画从右到左移除 View

ios - 旋转时,文本在 UITextView 行尾之前断到下一行

ios - View 加载成功后文本字段数据被删除

android - Flutter 应用程序与设备内置应用程序通信

ios - 带有 Retina-Tiles 的 MKTileOverlay

swift - 使用 Swift 在 MapView 上绘制多边形

iphone - 是否可以通过他在 ios 中的电话号码跟踪用户位置?

ios - 在 iOS 中,删除所有自定义注释并添加新的自定义注释的正确方法是什么?