【发布时间】:2011-05-12 19:42:25
【问题描述】:
我在 MKMapView 上使用 MKPinAnnotationView 注释引脚。在我正在开发的视图中,有一张以图钉为中心的地图。当我调用视图时,图钉会下拉,如果我点击它,它会显示注释信息 (canShowCallout = YES)。我如何在打开视图时获得此标注?无需单击注释图钉。
感谢阅读。
已编辑:
我正在使用此代码。
- (void)viewDidLoad {
[super viewDidLoad];
AddressAnnotation *annotation = [[[AddressAnnotation alloc] initWithCoordinate:self.currentAnnotationCoordinate] autorelease];
[self.mapView addAnnotation:annotation];
[self zoomCoordinate:self.currentAnnotationCoordinate];
}
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation {
// If it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
else { // Handles the other annotations.
// Try to dequeue an existing pin view first.
static NSString *AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (!pinView) {
// If an existing pin view was not available, creates one.
MKPinAnnotationView *customPinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
customPinView.animatesDrop = YES;
customPinView.canShowCallout = YES;
// Adds a detail disclosure button.
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView = rightButton;
return customPinView;
} else
pinView.annotation = annotation;
}
return nil;
}
- (void)mapViewDidFinishLoadingMap:(MKMapView *)theMapView {
AddressAnnotation *annotation = [[[AddressAnnotation alloc] initWithCoordinate:self.currentAnnotationCoordinate] autorelease];
for (id<MKAnnotation> currentAnnotation in mapView.annotations) {
if ([currentAnnotation isEqual:annotation]) {
[mapView selectAnnotation:currentAnnotation animated:FALSE];
}
}
}
已编辑:
调用 didAddAnnotationViews: 而不是 mapViewDidFinishLoadingMap:(正如 aBitObvious 评论的那样),它工作正常。
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
id<MKAnnotation> myAnnotation = [self.mapView.annotations objectAtIndex:0];
[self.mapView selectAnnotation:myAnnotation animated:YES];
}
【问题讨论】:
-
我最近回答了这样一个question。看看它是否适合你。
-
谢谢,像这样很好用。唯一的阴影是图钉在触摸地图之前显示了信息(我正在为下拉菜单设置动画)。之后怎么办?
-
使用 performSelector:withObject:afterDelay。见this answer。
-
有没有办法知道图钉何时接触到地图?
-
除非你自己画动画,否则我不知道。
标签: objective-c ios mkmapview mkpinannotationview