我在xcode中有一个mapview,它运行良好。
我的页面刚才是这样的:
这一切都很有效,我最终得到了一个带有大量针脚的mapview,以及一个包含这些针脚细节的tableview。
我现在要做的是,允许用户点击桌面视图中的一行,让地图缩放并居中到相应的地图图钉,然后自动激活注释图钉标注。
在我的'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];
这也很有用。点击表格行将缩放并将地图图钉固定在此位置。
我无法完成触发注释引脚标注的最后一步。
我试过了:
[mapview annotationsInMapRect:mapview.visibleMapRect];
但是这不起作用,可能在可见区域中可能还有2或3个地图引脚。
我需要做的是让引脚最靠近居中位置(见上文 - item.latitude / item.longitude)以自动打开它的标注。
代码中的所有内容都已设置并正常工作,并且地图引脚的标注在点击时会触发,我只需要让最靠近中心位置的引脚自动打开。
任何人都可以帮忙吗?
我在SO上尝试了各种其他建议,但似乎都没有符合这一要求。
答案 0 :(得分:1)
我想我已经找到了解决问题的方法,你需要使用这个[_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
}
}