【问题标题】:Showing the Pin Information by Default默认显示引脚信息
【发布时间】: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


【解决方案1】:

可能重复:How to trigger MKAnnotationView's callout view without touching the pin?

你想打电话给[mapView selectAnnotation:]

【讨论】:

  • 抱歉重复的问题。我在将注释添加到 viewDidLoad 方法后调用 selectAnnotation: 方法,但我仍然需要在 pin 上进行 clic 以显示信息。
  • 您是否尝试按照第一个答案的建议从 (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView 调用 selectAnnotation?
猜你喜欢
  • 2013-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多