我在 xcode 中有一个 mapview,一切正常。
我的页面刚才做的是这样的:
- 从后端数据库下载大量数据和位置
- 使用位置填充 map View 并放置图钉
- 填充 map View 下方的表格
一切都很好,我最终得到了一个带有大量图钉的 map View ,以及一个包含这些图钉详细信息的 TableView 。
我现在想做的是,允许用户点击表格 View 中的一行,让 map 缩放并居中到相应的 map 图钉,然后自动激活注释图钉标注。
在我的“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];
这也很好用。点击表格行将缩放到 map 图钉并将其居中放置在该位置。
我只是无法完成触发注释 pin 标注的最后一步。
我试过:
[mapview annotationsInMapRect:mapview.visibleMapRect];
但这是行不通的,可见区域中可能仍有 2 或 3 个 map 图钉。
我需要做的是让图钉最靠近中心位置(见上文 - item.latitude/item.longitude)以自动打开它的标注。
代码中的所有内容均已设置并正常工作, map 图钉具有在点击时触发的标注,我只需要最后一步让图钉最靠近中心位置自动打开。
有人可以帮忙吗?
我已经尝试过各种关于 SO 的其他建议,但似乎没有一个符合这个要求。
最佳答案
我想我已经找到了您的问题的解决方案,您需要使用这个 [_mapView setSelectedAnnotations:@[[[self.mapView comments] 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
}
}
关于iOS map View - 缩放以固定然后激活标注,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33317341/