如何将弹出视图显示为地图视图的注释?总览

时间:2011-04-07 14:16:52

标签: ios ipad

点击图钉时,在iPad上的地图应用中,您会获得带有“i”而不是公开指示符的正常注释。进一步点击“i”会显示一个像这样的弹出视图控制器。

map popover example

有没有办法轻松实现这个目标?

3 个答案:

答案 0 :(得分:58)

首先在地图中添加注释,然后在viewForAnnotation方法中,将rightCalloutAccessoryView设置为类型的按钮,例如,UIButtonTypeDetailDisclosure(我认为默认情况下蓝色信息按钮不可用)。

按下按钮将调用calloutAccessoryControlTapped委托方法。在此方法中,取消选择注释并显示弹出窗口。例如:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    [mapView deselectAnnotation:view.annotation animated:YES];

    YourContentViewController *ycvc = [[YourContentViewController alloc] init...
    UIPopoverController *poc = [[UIPopoverController alloc] initWithContentViewController:ycvc];
    [ycvc release];

    //hold ref to popover in an ivar
    self.annotationPopoverController = poc;

    //size as needed
    poc.popoverContentSize = CGSizeMake(320, 400);

    //show the popover next to the annotation view (pin)
    [poc presentPopoverFromRect:view.bounds inView:view 
        permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

    [poc release];
}

YourContentViewController是UIViewController的子类,您可以像任何其他视图控制器一样编码。地图应用程序看起来在内容中有UITableView。

答案 1 :(得分:2)

看来要为popover提供更好的位置,你必须从这个矩形中显示它:

CGPoint lc_point = [mapView convertCoordinate:view.annotation.coordinate toPointToView:mapView];
CGRect lc_frame = CGRectMake(lc_point.x,lc_point.y-view.frame.size.height,0,0);

答案 2 :(得分:2)

您可以使用像Gordon Hughes'Animated Callout这样的库。不幸的是,它仍然无法在iOS 6上完美运行(标注很奇怪)。

这是iOS 5:

enter image description here