iOS中的自定义注释图像

时间:2011-05-30 16:16:43

标签: objective-c ios annotations mapkit

我正在尝试用特定图像替换典型的iOS mapkit“pin”。我是编码的新手,所以我不确定为什么这不起作用,但这就是我的尝试:

方法

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
除其他外,我有这个:

pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
//pinView.pinColor=MKPinAnnotationColorPurple;
UIImage *pinImage = [UIImage imageNamed:@"Jeff.png"];
[pinView setImage:pinImage];

但是,即使代码编译得很好,pinView也没有设置pinImage。任何想法为什么不呢?

5 个答案:

答案 0 :(得分:3)

您所要做的就是将最后一行更改为:

[pinView addSubview:pinImage];

完整示例:

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

    MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ParkingPin"];

    UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pin_parking.png"]] autorelease];

    annView.animatesDrop = TRUE;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    [annView addSubview:imageView];
    return annView;
}

希望这有帮助!

答案 1 :(得分:3)

图像应设置为按钮而不是annView,我使用如下:

UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[button setImage:[UIUtils getStyleImage:@"page_map_accessory_button.png"] forState:UIControlStateNormal];
[button setImage:[UIUtils getStyleImage:@"page_map_accessory_button.png"] forState:UIControlStateSelected];
[button setImage:[UIUtils getStyleImage:@"page_map_accessory_button.png"] forState:UIControlStateHighlighted];
pinView.rightCalloutAccessoryView = button;

答案 2 :(得分:2)

我在这里没有看到的答案是,您必须在viewForAnnotation方法中出列或创建MKAnnotationView,并将您的类作为地图视图的委托,以便它调用此方法来获取视图。

如果您在其他地方创建了注释视图,则不会显示使用image属性设置的任何自定义图像,并且您只会看到一个图钉。

答案 3 :(得分:1)

只需从代码中删除“animatesDrop”行即可。它会对你有用。

答案 4 :(得分:0)

只是做:

annView.image=[UIImage imageNamed:@"imagename.png"];
相关问题