在 MKAnnotationPinView 中设置图像

发布于 2024-10-23 12:50:09 字数 1952 浏览 7 评论 0原文

我在委托内有以下代码:

     - (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)anAnnotation 
{
    MKPinAnnotationView *pin = (MKPinAnnotationView *) [map dequeueReusableAnnotationViewWithIdentifier: @"RoutePin"];
    if (pin == nil)
    {
        if ([anAnnotation isKindOfClass:[RouteMapAnnotation class]])
        {
            RouteMapAnnotation *theAnnotation = (RouteMapAnnotation *)anAnnotation;
            if (theAnnotation.identifier == @"routePin")
            {
                //NSLog(@"TESTING PART III");
                MKPinAnnotationView *startAnnotationPin = [[MKPinAnnotationView alloc] initWithAnnotation:anAnnotation reuseIdentifier:@"RoutePin"];
                UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
                startAnnotationPin.canShowCallout = YES;
                startAnnotationPin.animatesDrop = YES;

                startAnnotationPin.rightCalloutAccessoryView = rightButton;
                startAnnotationPin.pinColor = MKPinAnnotationColorRed;
                return startAnnotationPin;
            }
            else if (theAnnotation.identifier == @"finishPin")
            {
                NSLog(@"CREATING FINISH FLAG PRIOR");
                MKPinAnnotationView *finishAnnotationPin = [[MKPinAnnotationView alloc] initWithAnnotation:anAnnotation reuseIdentifier:@"FinishPin"];
                finishAnnotationPin.canShowCallout = NO;
                finishAnnotationPin.animatesDrop = YES;
                //finishAnnotationPin.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://cdn4.iconfinder.com/data/icons/formula1/f1_png/128/checkered_flag.png"]]];
                finishAnnotationPin.image = [UIImage imageNamed:@"flag_finish"];
                return finishAnnotationPin;

            }
        }
    }
    return nil;
}

但是它没有在地图上显示图钉的图像。我缺少什么?

I have the following code inside the delegate:

     - (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)anAnnotation 
{
    MKPinAnnotationView *pin = (MKPinAnnotationView *) [map dequeueReusableAnnotationViewWithIdentifier: @"RoutePin"];
    if (pin == nil)
    {
        if ([anAnnotation isKindOfClass:[RouteMapAnnotation class]])
        {
            RouteMapAnnotation *theAnnotation = (RouteMapAnnotation *)anAnnotation;
            if (theAnnotation.identifier == @"routePin")
            {
                //NSLog(@"TESTING PART III");
                MKPinAnnotationView *startAnnotationPin = [[MKPinAnnotationView alloc] initWithAnnotation:anAnnotation reuseIdentifier:@"RoutePin"];
                UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
                startAnnotationPin.canShowCallout = YES;
                startAnnotationPin.animatesDrop = YES;

                startAnnotationPin.rightCalloutAccessoryView = rightButton;
                startAnnotationPin.pinColor = MKPinAnnotationColorRed;
                return startAnnotationPin;
            }
            else if (theAnnotation.identifier == @"finishPin")
            {
                NSLog(@"CREATING FINISH FLAG PRIOR");
                MKPinAnnotationView *finishAnnotationPin = [[MKPinAnnotationView alloc] initWithAnnotation:anAnnotation reuseIdentifier:@"FinishPin"];
                finishAnnotationPin.canShowCallout = NO;
                finishAnnotationPin.animatesDrop = YES;
                //finishAnnotationPin.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://cdn4.iconfinder.com/data/icons/formula1/f1_png/128/checkered_flag.png"]]];
                finishAnnotationPin.image = [UIImage imageNamed:@"flag_finish"];
                return finishAnnotationPin;

            }
        }
    }
    return nil;
}

However it's not showing the image for the pin on the map. What am I missing??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撧情箌佬 2024-10-30 12:50:09

您应该使用 MKAnnotationView 而不是 MKPinAnnotationView。

引脚注释用于引脚。

You should use MKAnnotationView instead of MKPinAnnotationView.

pin annotation is for pins.

秋日私语 2024-10-30 12:50:09

另请注意,MKPinAnnotationView 确实为常规 MKAnnotationView 提供了一些附加功能,例如拖动时的动画和 3d 阴影效果。如果您使用 MKAnnotationView,您将不会得到这些。

如果您需要这些内置功能,您可以创建一个 UIImageView 并将其作为子视图添加到 MKPinAnnotationView 中。这将为您提供一个看起来像您想要的任何注释;但行为就像一个别针。我用它来用我自己的图像替换图钉的头部。

Also note that MKPinAnnotationView does offer some additional functionality to a regular MKAnnotationView, such as animating while dragging and the 3d shadow effect. You won't get these if you use MKAnnotationView.

If you want these built-in features, you can create a UIImageView and add it as a subview to your MKPinAnnotationView. This will give you an annotation that looks like whatever you want; but behaves like a pin. I use it to replace the head of the pin with my own images.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文