【问题标题】:How to open call out MKAnnotationView programmatically? (iPhone, MapKit)如何以编程方式打开调用 MKAnnotationView? (iPhone, MapKit)
【发布时间】:2010-02-03 17:16:43
【问题描述】:

我想以编程方式打开 MKPinAnnotationView 的标注。例如,我在地图上放了 10 个图钉,并想打开离我最近的一个图钉。我该怎么做呢?

Apple 已经为 MKAnnotationView's 指定了 'selected' 参数,但不鼓励直接设置(这不起作用,尝试过)。

对于其余的MKAnnotationView只有一个setHighlighted(同样的故事),可以ShowCallout方法..

如果这可能的话,有什么提示吗?

【问题讨论】:

    标签: iphone ios mapkit mkannotation callouts


    【解决方案1】:

    在你的 mapViewController 中创建一个动作方法:

    - (void)openAnnotation:(id)annotation 
    {
        //mv is the mapView
        [mv selectAnnotation:annotation animated:YES];
    
    }
    

    然后您可以根据当前位置确定最近的注释并遍历数组中可用的注释。

    [mv annotations];
    

    计算出最接近的注解后,调用:

    [self openAnnotation:closestAnnotation];
    

    mapView 应该自动滚动以将您的注释放置在显示区域的中心。

    【讨论】:

    • 感谢 Nate,修正了错字。
    • 如果您知道要以编程方式选择的注释将在地图视图的可见区域中,您可以使用[mv annotationsInMapRect:mv.visibleMapRect]。如果您正在处理大量地图注释,这将提高性能。
    • 是的。自最初提供此答案以来,MapKit API 已经扩展。 annotationsInMapRect: 是在 iOS 4.2 中添加的。
    【解决方案2】:

    在 swift 3 中更新为:

    func openAnnotation(annotation: MkAnnotation) {
    _ = [mapView .selectAnnotation(annotation, animated: true)]
    }
    

    并且可以使用任何注释调用(这将打开注释标注视图并尝试在地图上将注释居中)

    例如在假设的注释列表中使用第二个注释。

    openAnnotation(annotation: mapView.annotations[1])
    

    【讨论】:

    • 您的代码是 ObjC(方括号)和 Swift 的奇怪组合。为什么要创建一个数组然后丢弃它?
    猜你喜欢
    • 2012-05-31
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多