MKAnnotationView 未显示在地图上
我正在尝试使用 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 无法出现在地图上。 RootViewController
是 mapView
的委托,因此 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
viewForAnnotation
中,创建一个MKPinAnnotationView
而不是MKAnnotationView
(您必须为其设置image
)。尽管对于默认的引脚注释视图,您根本不需要实现委托方法。
In
viewForAnnotation
, create anMKPinAnnotationView
instead of anMKAnnotationView
(for which you have to set theimage
).Although for a default pin annotation view, you don't need to implement the delegate method at all.