iOS 地图视图 - 缩放以固定然后激活标注

iOS mapview - zoom to pin and then activate callout

我在 xcode 中有一个地图视图,一切正常。

我的页面刚才是这样的:

  1. 从后端数据库下载大量数据和位置
  2. 使用位置填充地图视图并放置图钉
  3. 在地图视图下方填充一个 table

这一切都很好,我最终得到了一个带有大量图钉的地图视图,以及一个包含这些图钉详细信息的 table视图。

我现在想做的是,允许用户点击 table 视图中的一行,使地图缩放并居中到相应的地图图钉,然后自动激活注释图钉标注。

在我的 'didselectrow' 方法中,我有以下内容:

MKCoordinateSpan span = MKCoordinateSpanMake(0.1f, 0.1f);
CLLocationCoordinate2D coordinate = { [item.latitude floatValue], [item.longitude floatValue] };
MKCoordinateRegion region = { coordinate, span };
[self.mapView setRegion:region animated:YES];

这也很好用。点击 table 行将放大地图图钉并将其居中。

我只是无法完成触发注释图钉标注的最后一步。

我试过:

[mapview annotationsInMapRect:mapview.visibleMapRect];

但这不起作用,并且可能在可见区域中仍然有 2 或 3 个地图图钉。

我需要做的是让图钉最靠近中心位置(见上文 - item.latitude / item.longitude)以自动打开它的标注。

代码中的所有内容均已设置并正常工作,地图图钉具有点击时会触发的标注,我只需要最后一步让图钉最接近中心位置自动打开。

有人可以帮忙吗?

我已经尝试过关于 SO 的各种其他建议,但 none 似乎符合这个要求。

我想我已经找到了解决你问题的方法,你需要使用这个 [_mapView setSelectedAnnotations:@[[[self.mapView annotations] lastObject]]];

为了测试我有 created an small project that have these 2 methods.

- (IBAction)buttonTouched:(id)sender {
    [_mapView showAnnotations:[self.mapView annotations] animated:YES];
    [self performSelector:@selector(showAnnotationCallOut) withObject:nil afterDelay:1.0f];
}

- (void) showAnnotationCallOut {
    [_mapView setSelectedAnnotations:@[[[self.mapView annotations] lastObject]]];
}

注意:我只调用了一个注释来测试我为什么调用最后一个对象。您需要为注释数组的特定注释调用它。

编辑: 根据 Richerd 的评论,这里是找到注释并显示标注的问题的解决方案。

    for (MapViewAnnotation *annotion in [self.mapView annotion]) {
        if ([annotion.identifire isEqualToString:annotationToCallCallOutIdentifier]) {
            //[_mapView setSelectedAnnotations:@[annotation]];
            [_mapView selectAnnotation:annotation animated:YES];
            break;//don't break if there are can be more than one callouts
        }
    }