默认显示 Pin 信息

发布于 2024-10-03 06:01:39 字数 2748 浏览 1 评论 0原文

我正在通过 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];
}

I am working with MKPinAnnotationView annotations pins over a MKMapView. Into the view that I am developing, there is a map centered on a pin. When I call the view the pin drops down and if I make a click over it, it shows the annotation information (canShowCallout = YES). How I can get this callout just when I open the view? Without making a click over the annotation pin.

Thanks for reading.

Edited:

I am using this code.

- (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];
        }
    }
}

Edited:

Calling didAddAnnotationViews: instead of mapViewDidFinishLoadingMap: (as aBitObvious has commented), it works fine.

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {

    id<MKAnnotation> myAnnotation = [self.mapView.annotations objectAtIndex:0];
    [self.mapView selectAnnotation:myAnnotation animated:YES];
}

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

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

发布评论

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

评论(1

仄言 2024-10-10 06:01:39

可能重复:如何在不触摸的情况下触发 MKAnnotationView 的标注视图pin?

您想要调用[mapView selectAnnotation:]

Possible duplicate: How to trigger MKAnnotationView's callout view without touching the pin?

You want to call [mapView selectAnnotation:].

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