MKAnnotationView 未显示在地图上

发布于 2024-12-17 20:13:21 字数 1338 浏览 5 评论 0原文

我正在尝试使用 MKPointAnnotation 注释我的地图,如下所示:

- (void)viewDidLoad
{
    NSLog(@"RootViewController viewDidLoad");

    [super viewDidLoad];

    CLLocationCoordinate2D coord = 
    CLLocationCoordinate2DMake(54.903683,23.895435);

    MKPointAnnotation *toyAnnotation = [[MKPointAnnotation alloc] init];

    toyAnnotation.coordinate = coord;
    toyAnnotation.title = @"Title";
    toyAnnotation.subtitle = @"Subtitle";

    [mapView addAnnotation:toyAnnotation];

    [toyAnnotation release];

}

- (MKAnnotationView *)mapView:(MKMapView *)m 
            viewForAnnotation:(id <MKAnnotation>)annotation
{
    NSLog(@"RootViewController mapView: viewForAnnotation:");
    NSLog(@"%@",annotation);

    MKAnnotationView *pin = [[MKAnnotationView alloc] 
                             initWithAnnotation:annotation 
                             reuseIdentifier:nil];

    pin.enabled = YES;
    pin.canShowCallout = YES;

    return [pin autorelease];
}

Pin 无法出现在地图上。 RootViewControllermapView 的委托,因此 mapView:viewForAnnotation: 方法被调用:

2011-11-24 15:04:03.808 App[2532:707] RootViewController mapView: viewForAnnotation:
2011-11-24 15:04:03.810 App[2532:707] <MKPointAnnotation: 0x1885c0>

我做错了什么以及如何解决此问题?

I am trying to annotate my map with MKPointAnnotation, like this:

- (void)viewDidLoad
{
    NSLog(@"RootViewController viewDidLoad");

    [super viewDidLoad];

    CLLocationCoordinate2D coord = 
    CLLocationCoordinate2DMake(54.903683,23.895435);

    MKPointAnnotation *toyAnnotation = [[MKPointAnnotation alloc] init];

    toyAnnotation.coordinate = coord;
    toyAnnotation.title = @"Title";
    toyAnnotation.subtitle = @"Subtitle";

    [mapView addAnnotation:toyAnnotation];

    [toyAnnotation release];

}

- (MKAnnotationView *)mapView:(MKMapView *)m 
            viewForAnnotation:(id <MKAnnotation>)annotation
{
    NSLog(@"RootViewController mapView: viewForAnnotation:");
    NSLog(@"%@",annotation);

    MKAnnotationView *pin = [[MKAnnotationView alloc] 
                             initWithAnnotation:annotation 
                             reuseIdentifier:nil];

    pin.enabled = YES;
    pin.canShowCallout = YES;

    return [pin autorelease];
}

Pin fails to appear on the map. RootViewController is a delegate for mapView and thus mapView:viewForAnnotation: method gets called:

2011-11-24 15:04:03.808 App[2532:707] RootViewController mapView: viewForAnnotation:
2011-11-24 15:04:03.810 App[2532:707] <MKPointAnnotation: 0x1885c0>

What am I doing wrong and how to fix this issue?

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

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

发布评论

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

评论(1

娇纵 2024-12-24 20:13:21

viewForAnnotation 中,创建一个 MKPinAnnotationView 而不是 MKAnnotationView(您必须为其设置 image)。

尽管对于默认的引脚注释视图,您根本不需要实现委托方法。

In viewForAnnotation, create an MKPinAnnotationView instead of an MKAnnotationView (for which you have to set the image).

Although for a default pin annotation view, you don't need to implement the delegate method at all.

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